Java 如何将布尔值更改为int publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); 布尔数; 做{ System.out.println(“欢迎参加IP数学测验”); System.out.println(“***主菜单***”); 系统输出打印项(“1)添加项”); System.out.println(“2)减法”); System.out.println(“3)模数”); 系统输出打印(“4)分部”); System.out.println(“5)Randon”); System.out.println(“9)Quit”); if(input.hasNextInt()){ isNumber=true; }否则{ System.out.println(“错误!输入仅接受整数。输入1-5或9”); isNumber=false; input.next(); } }而(!(isNumber)); 扫描仪=新的扫描仪(System.in); System.out.printf(“>”); System.out.println(“问题数量”); int corr_num=0; int可能; 开关(isNumber){ 案例1: int g=scanner.nextInt(); 对于(int i=1;i
我假设您尝试使用基于用户输入的switch语句。因此将有两种以上的情况(可能是9)。(如果您还不知道bool只能包含两种情况true或false,如果您将其转换为int,它将是0或其他)将布尔值转换为int不适合这种情况。因此,您不应该尝试将布尔值转换为int。请执行以下操作。在switch语句中添加Java 如何将布尔值更改为int publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); 布尔数; 做{ System.out.println(“欢迎参加IP数学测验”); System.out.println(“***主菜单***”); 系统输出打印项(“1)添加项”); System.out.println(“2)减法”); System.out.println(“3)模数”); 系统输出打印(“4)分部”); System.out.println(“5)Randon”); System.out.println(“9)Quit”); if(input.hasNextInt()){ isNumber=true; }否则{ System.out.println(“错误!输入仅接受整数。输入1-5或9”); isNumber=false; input.next(); } }而(!(isNumber)); 扫描仪=新的扫描仪(System.in); System.out.printf(“>”); System.out.println(“问题数量”); int corr_num=0; int可能; 开关(isNumber){ 案例1: int g=scanner.nextInt(); 对于(int i=1;i,java,Java,我假设您尝试使用基于用户输入的switch语句。因此将有两种以上的情况(可能是9)。(如果您还不知道bool只能包含两种情况true或false,如果您将其转换为int,它将是0或其他)将布尔值转换为int不适合这种情况。因此,您不应该尝试将布尔值转换为int。请执行以下操作。在switch语句中添加input.nextInt() public static void main(String[] args) { Scanner input = new Scanner(System.in)
input.nextInt()
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 (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;
}
}
用户输入的数字分别是数字或字符,如果用户输入的是数字,则转到案例,否则最好输入用户输入。@Nekoten那么上述代码应该有效,而不是转到案例描述您试图实现的问题,这样人们就可以理解并建议更好的方法。
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;
}
}
}
switch (isNumber?0:1)