下面列出了java.util.Scanner#nextBoolean ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter three sides of a triangle a color
// and a Boolean value to indicate whether the triangle is filled
System.out.print("Enter three sides of the triangle: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
System.out.print("Enter a color: ");
String color = input.next();
System.out.print("Is the triangle filled (true / false)? ");
boolean filled = input.nextBoolean();
// Create a Triangle
Triangle triangle = new Triangle(side1, side2, side3, color, filled);
System.out.println(triangle);
}
public boolean getBool(String key, boolean def) {
String str = readKeyFile(key);
Scanner scan = new Scanner(str);
if (scan.hasNextBoolean()) {
return scan.nextBoolean();
} else {
return def;
}
}
/**
* @param str string to check
* @return whether the string is a booelan type
*/
public static boolean isBoolean(String str) {
if (str == null) {
return false;
}
Scanner s = new Scanner(str.trim());
if (!s.hasNextBoolean()) {
return false;
}
s.nextBoolean();
return !s.hasNext();
}
/**
* Parse a boolean with a default value.
*
* @param str string to parse
* @param def default boolean value
* @return parsed boolean
*/
public static Boolean parseBoolean(String str, Boolean def) {
if (str == null) {
return def;
}
Scanner scnr = new Scanner(str);
if (!scnr.hasNextBoolean()) {
return def;
}
return scnr.nextBoolean();
}
/**
*
* @param val
* @return
* @throws IllegalArgumentException
*/
public Object interpret(Object val) throws IllegalArgumentException {
String s = val.toString();
Scanner sc = new Scanner(s);
return sc.hasNextLong() ? sc.nextLong()
: sc.hasNextInt() ? sc.nextInt()
: sc.hasNextDouble() ? sc.nextDouble()
: sc.hasNextBoolean() ? sc.nextBoolean()
: sc.hasNextBigInteger() ? sc.nextBigInteger()
: sc.hasNextFloat() ? sc.nextFloat()
: sc.hasNextByte() ? sc.nextByte()
: sc.hasNext() ? sc.next()
: s;
}
/**
*
* @param val
* @return
* @throws IllegalArgumentException
*/
public Object interpret(Object val) throws IllegalArgumentException {
String s = val.toString();
Scanner sc = new Scanner(s);
return sc.hasNextLong() ? sc.nextLong()
: sc.hasNextInt() ? sc.nextInt()
: sc.hasNextDouble() ? sc.nextDouble()
: sc.hasNextBoolean() ? sc.nextBoolean()
: sc.hasNextBigInteger() ? sc.nextBigInteger()
: sc.hasNextFloat() ? sc.nextFloat()
: sc.hasNextByte() ? sc.nextByte()
: sc.hasNext() ? sc.next()
: s;
}
public Object interpret(Object val) throws IllegalArgumentException {
String s = val.toString();
Scanner sc = new Scanner(s);
return sc.hasNextInt() ? sc.nextInt()
: sc.hasNextLong() ? sc.nextLong()
: sc.hasNextDouble() ? sc.nextDouble()
: sc.hasNextBoolean() ? sc.nextBoolean()
: sc.hasNextBigInteger() ? sc.nextBigInteger()
: sc.hasNextFloat() ? sc.nextFloat()
: sc.hasNextByte() ? sc.nextByte()
: sc.hasNext() ? sc.next()
: s;
}
public Country(Scanner in) {
if (in.hasNextLine()) {
this.name = in.next();
this.population = in.nextInt();
this.area = in.nextInt();
this.landlocked = in.nextBoolean();
}
}
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter three sides of the triangle
System.out.print("Enter three side of the triangle: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
// Prompt the user to enter a color
System.out.print("Enter a color: ");
String color = input.next();
// Prompt the user to enter whether the triangle is filled
System.out.print("Is the triangle filled (true / false)? ");
boolean filled = input.nextBoolean();
// Create triangle object with user input
Triangle triangle = new Triangle(side1, side2, side3);
triangle.setColor(color);
triangle.setFilled(filled);
System.out.println(triangle.toString());
System.out.println("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Color: " + triangle.getColor());
System.out.println("Triangle is" + (triangle.isFilled() ? "" : " not ")
+ "filled");
}
/**
* Parses a string encoded KRL variable to a JOpenShowVar Variable
*
* @param callback the Callback from the robot
* @return the JOpenShowVar Variable
* @throws NumberFormatException on parsing error
*/
public static Variable parseVariable(Callback callback) throws NumberFormatException {
String variable = callback.getVariableName();
String value = callback.getStringValue();
int id = callback.getId();
long readTime = callback.getReadTime();
// int option = callback.getOption();
Scanner sc = new Scanner(value);
Variable var;
if (sc.hasNextInt()) {
var = new Int(id, variable, sc.nextInt(), readTime);
sc.close();
} else if (sc.hasNextFloat()) {
var = new Real(id, variable, (double) sc.nextFloat(), readTime);
sc.close();
} else if (sc.hasNextDouble()) {
var = new Real(id, variable, sc.nextDouble(), readTime);
sc.close();
} else if (sc.hasNextBoolean()) {
var = new Bool(id, variable, sc.nextBoolean(), readTime);
sc.close();
}else if (value.contains("#")) {
var = new Enum(id, variable, sc.nextLine(), readTime);
sc.close();
} else if (value.contains("{")) {
sc.close();
var = new Struct(id, variable, Struct.parseString(value), readTime);
} else {
var = new Real(id, variable, Double.parseDouble(value), readTime);
sc.close();
}
return var;
}
/**
* Parses a String encoded KRL Struct to a JOpenShowVar Struct
* @param value the KRL String encoded Struct
* @return a JOpenShowVar Struct
*/
public static List<StructNode> parseString(String value) {
if (value.contains(":")) {
value = value.substring(value.indexOf(":") + 1, value.indexOf("}") + 1);
} else {
value = value.substring(value.indexOf("{") + 1, value.indexOf("}") + 1);
value = " " + value;
}
value = value.replaceAll("\\{", "").replaceAll("\\}", "");
List<StructNode> nodes = new ArrayList<>();
String[] split1 = value.split(",");
for (String str1 : split1) {
String[] split2 = str1.split(" ");
String name = split2[1];
Object val;
Scanner sc = new Scanner(split2[2]);
if (sc.hasNextInt()) {
val = sc.nextInt();
} else if (sc.hasNextDouble()) {
val = sc.nextDouble();
} else if (sc.hasNextBoolean()) {
val = sc.nextBoolean();
} else {
val = sc.next();
}
StructNode node = new StructNode(name, val);
nodes.add(node);
}
return nodes;
}
public static boolean readBoolean() throws Exception {
//напишите тут ваш код
Scanner sc = new Scanner(System.in);
return sc.nextBoolean();
}
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter a color
System.out.print("Enter a color: ");
String color = input.next();
// Prompt the user to enter whether the triangle is filled
System.out.print("Is the triangle filled (true / false)? ");
boolean filled = input.nextBoolean();
boolean repeatSidesInput = true;
do {
// Prompt the user to enter three sides of the triangle
System.out.print("Enter three side of the triangle: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
try {
// Create triangle object with user input
Triangle triangle = new Triangle(side1, side2, side3);
repeatSidesInput = false;
triangle.setColor(color);
triangle.setFilled(filled);
System.out.println(triangle.toString());
System.out.println("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Color: " + triangle.getColor());
System.out.println("Triangle is" + (triangle.isFilled() ? "" : " not ")
+ "filled");
}
catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
} while (repeatSidesInput);
}