首页 > 解决方案 > 如何将布尔值更改为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。

标签: java

解决方案


我假设您正在尝试根据用户输入使用 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)

推荐阅读