Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在我的if-else语句之后忽略Switch语句 //扫描仪 扫描仪扫描=新扫描仪(System.in); //变数 双倍体重指数;//体质指数 双倍重量;//重量(千克) 双高;//高度(米) 字符串[]分类={“体重不足”、“正常”、“超重”、“肥胖”}; System.out.print(“您的体重,单位为KG\n”); 权重=scan.nextDouble(); System.out.print(“以米为单位输入高度:\n”); 高度=scan.nextDouble(); 体重指数=体重/(身高*身高); 如果(体重指数_Java_Arrays - Fatal编程技术网

Java 在我的if-else语句之后忽略Switch语句 //扫描仪 扫描仪扫描=新扫描仪(System.in); //变数 双倍体重指数;//体质指数 双倍重量;//重量(千克) 双高;//高度(米) 字符串[]分类={“体重不足”、“正常”、“超重”、“肥胖”}; System.out.print(“您的体重,单位为KG\n”); 权重=scan.nextDouble(); System.out.print(“以米为单位输入高度:\n”); 高度=scan.nextDouble(); 体重指数=体重/(身高*身高); 如果(体重指数

Java 在我的if-else语句之后忽略Switch语句 //扫描仪 扫描仪扫描=新扫描仪(System.in); //变数 双倍体重指数;//体质指数 双倍重量;//重量(千克) 双高;//高度(米) 字符串[]分类={“体重不足”、“正常”、“超重”、“肥胖”}; System.out.print(“您的体重,单位为KG\n”); 权重=scan.nextDouble(); System.out.print(“以米为单位输入高度:\n”); 高度=scan.nextDouble(); 体重指数=体重/(身高*身高); 如果(体重指数,java,arrays,Java,Arrays,输出,,my switch语句在if-else语句之后不起作用。它会忽略所有内容,并直接跳到我的开关中的默认值。而我的意图是在if-else之后添加一些文本。所以,基本上,如果我的计算结果显示我超重了,那么我的switch语句现在必须跳转到case“overweighted”并打印出那段代码。。我做错了什么 数组的结果。toString(分类)的[体重不足、正常、超重、肥胖]与任何开关情况都不匹配 解决方案可以是这样的: //Scanner Scanner scan = new

输出,,my switch语句在if-else语句之后不起作用。它会忽略所有内容,并直接跳到我的开关中的默认值。而我的意图是在if-else之后添加一些文本。所以,基本上,如果我的计算结果显示我超重了,那么我的switch语句现在必须跳转到case“overweighted”并打印出那段代码。。我做错了什么

数组的结果。toString(分类)的[体重不足、正常、超重、肥胖]与任何开关情况都不匹配

解决方案可以是这样的:

//Scanner
        Scanner scan = new Scanner(System.in);

        //Variables
        double bmi;                        // Body Mass Index
        double weight;                     // Weight in kilograms
        double height;                     // Height in meters
        String[] classification = {"Underweight", "Normal", "Overweight", "Obese"};

        System.out.print("Your weight in KG \n");
        weight = scan.nextDouble();
        System.out.print("Enter height in meters: \n");
        height = scan.nextDouble();
        bmi = weight / (height * height);

        if (bmi < 18.5) {
            System.out.print("You're " + classification[0] + "\n");
        } else if (bmi < 25) {
            System.out.print("You're " + classification[1] + "\n");
        } else if (bmi < 30) {
            System.out.print("You're " + classification[2] + "\n");
        } else {
            System.out.print("You're " + classification[3] + "\n");
        }

        switch (Arrays.toString(classification)) {
            case "Underweight":
                System.out.println("Underweight");
                break;
            case "Normal":
                System.out.println("Normal");
                break;
            case "Overweight":
                System.out.println("A bit overweighted");
                break;
            case "Obese":
                System.out.println("A bit obese");
                break;
            default:
                System.out.println("Ok");
                break;
        }
    }
字符串结果=null;
如果(体重指数<18.5){
System.out.print(“您是”+分类[0]+“\n”);
结果=分类[0];
}否则,如果(体重指数<25){
System.out.print(“您是”+分类[1]+“\n”);
结果=分类[1];
}否则,如果(体重指数<30){
System.out.print(“您是”+分类[2]+“\n”);
结果=分类[2];
}否则{
System.out.print(“您是”+分类[3]+“\n”);
结果=分类[3];
}
开关(结果){
“体重不足”案例:
系统输出打印项次(“重量不足”);
打破
“正常”情况:
系统输出打印项次(“正常”);
打破
“超重”一案:
System.out.println(“有点超重”);
打破
“肥胖”病例:
System.out.println(“有点肥胖”);
打破
违约:
System.out.println(“Ok”);
打破
}

数组。toString(分类)
将始终包含
[]
数组。toString(分类)
将打印
[体重不足、正常、超重、肥胖]
,并且永远不会匹配您的任何案例感谢您的快速回复!放置[]将给我下一个错误“class.”预期切换
数组的结果没有意义。toString(classification)
。它(实际上)是一个常数,所以每次都会做相同的事情。我不知道,在开关的情况下,单个案例是如何选择的。您需要为单个BMI进行特定分类。例如,创建一个变量,该变量填充在If-Else中,并在开关盒中使用。您还可以移动
System.out.print(“您”+classification[X]+“\n”)
if
s中取出,并执行单个
系统输出打印(“您是”+result+“\n”)之后。如果我添加一个字符串结果,我会得到一个错误。它表示我的字符串结果尚未初始化;将使if-else输出为null,因此也不会响应我的开关。@Jaws将代码编辑为如下内容:
String result=null
字符串结果=”谢谢@mohsenJsh解决了我的问题!我忽略了结果=分类[0]。再次感谢!
String result = null;
if (bmi < 18.5) {
        System.out.print("You're " + classification[0] + "\n");
        result = classification[0];
    } else if (bmi < 25) {
        System.out.print("You're " + classification[1] + "\n");
        result = classification[1];
    } else if (bmi < 30) {
        System.out.print("You're " + classification[2] + "\n");
        result = classification[2];
    } else {
        System.out.print("You're " + classification[3] + "\n");
        result = classification[3];
    }

    switch (result) {
        case "Underweight":
            System.out.println("Underweight");
            break;
        case "Normal":
            System.out.println("Normal");
            break;
        case "Overweight":
            System.out.println("A bit overweighted");
            break;
        case "Obese":
            System.out.println("A bit obese");
            break;
        default:
            System.out.println("Ok");
            break;
    }