`
public static void main(String [] args){扫描仪输入=新的Scanner(System.in);
boolean isNumber;
do {
System.out.println("Welcome to IP Math Quiz");
System.out.println("***Main Menu***");
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Modulus");
System.out.println("4) Division");
System.out.println("5) Randon");
System.out.println("9) Quit");
if (input.hasNextInt()) {
isNumber = true;
} else {
System.out.println("Error ! Input accept integer only.Input 1-5 or 9 ");
isNumber = false;
input.next();
}
} while (!(isNumber));
Scanner scanner = new Scanner(System.in);
System.out.printf(">");
System.out.println("Number of Question");
int corr_num = 0;
int possible;
switch (isNumber) {
case 1:
int g = scanner.nextInt();
for (int i = 1; i <= g; i++) {
int Add_x = (int) (Math.random() * 99); // To random the value of x
int Add_y = (int) (Math.random() * 99);// To random the value of y
System.out.println("Q" + i + ":" + " " + Add_x + " " + " + " + " " + Add_y + " " + "=");
int Add_user_input = scanner.nextInt();
int check;
check = (Add_x + Add_y);
if (Add_user_input == check) {
System.out.println("You are correct!");
corr_num = corr_num + 1;
possible = ((g - corr_num) / corr_num) * 100;
System.out.println("The right possible is " + possible + " % ");
} else {
System.out.println(" Sorry, the right answer is : " + check);
}
}
break;
}
}
}
`
用户输入的数字分别是数字或字符,如果用户输入的是数字,则输入用户名。
我假设您正在尝试根据用户输入使用switch语句。因此,将有2个以上的案例(可能是9个)。 (如果您还不知道bool只能容纳两种情况下的true或false,并且如果将其转换为int,则它将为0或类似的值)将boolean转换为int不适合这种情况。因此,您不应该尝试将bool转换为int。而是执行以下操作。在switch语句中添加input.nextInt()
。
package com.company;
import java.util.Scanner;
public class demo7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean isNumber;
do {
System.out.println("Welcome to IP Math Quiz");
System.out.println("***Main Menu***");
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Modulus");
System.out.println("4) Division");
System.out.println("5) Randon");
System.out.println("9) Quit");
if (input.hasNextInt()) {
isNumber = true;
} else {
System.out.println("Error ! Input accept integer only.Input 1-5 or 9 ");
isNumber = false;
input.next();
}
} while (!(isNumber));
Scanner scanner = new Scanner(System.in);
System.out.printf(">");
System.out.println("Number of Question");
int corr_num = 0;
int possible;
switch (input.nextInt()) {
case 1:
int g = scanner.nextInt();
for (int i = 1; i <= g; i++) {
int Add_x = (int) (Math.random() * 99); // To random the value of x
int Add_y = (int) (Math.random() * 99);// To random the value of y
System.out.println("Q" + i + ":" + " " + Add_x + " " + " + " + " " + Add_y + " " + "=");
int Add_user_input = scanner.nextInt();
int check;
check = (Add_x + Add_y);
if (Add_user_input == check) {
System.out.println("You are correct!");
corr_num = corr_num + 1;
possible = ((g - corr_num) / corr_num) * 100;
System.out.println("The right possible is " + possible + " % ");
} else {
System.out.println(" Sorry, the right answer is : " + check);
}
}
break;
}
}
}
如果确实需要将bool转换为int,则您可以做的最好的(imo)与以下类似
switch (isNumber?0:1)