java - 如何将布尔值更改为int
问题描述
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;
}
}
用户输入的数字分别是数字或者字符,如果用户输入的是数字则去case,否则就是reputed进入user_input。
解决方案
我假设您正在尝试根据用户输入使用 switch 语句。所以会有超过 2 个案例(可能是 9 个)。(如果您还不知道 bool 只能容纳两种情况,true 或 false,如果您将其转换为 int 它将是 0 或其他东西)将 boolean 转换为 int 不适合这种情况。所以你不应该尝试将 bool 转换为 int。而是执行以下操作。加入input.nextInt()
switch语句。
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)
推荐阅读
- python - 有什么方法可以缩短类属性字符串吗?
- c# - 具有依赖关系的工厂方法模式的实现
- javascript - React - 创建和更新数据库模型、继承或组合的组件?
- google-apps-script - 仅从一张纸复制值并粘贴到另一张纸上
- c - 为什么 argv 的第一个参数是 1 而不是 0
- ruby-on-rails - Capistrano/Passenger 错误@nginx 使用 nokogiri - 库版本不兼容
- java - 需要帮助计算驾驶汽油成本
- vue.js - 发生父级更新时,子级发出事件会冻结 UI
- javascript - 登录到 Chrome 控制台的对象具有不存在的属性
- xml - PowerShell XML 导出到 CSV - 无法获得一些子值