未在JAVA中输入切换案例

问题描述 投票:0回答:3

我要解决的任务是,我必须在3nums整数的数字之间找到最大的和。

我决定使用“开关盒”来做,因为我仍然是新手。但是,不幸的是,当我运行它时,它会在循环后直接跳过。这是我的代码:

import java.util.Scanner;

public class Game {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int nums = Integer.parseInt(input);
        int firstNum, secondNum, thirdNum, sumNums, sumNums2 = 0;

        firstNum = Character.getNumericValue(input.charAt(0));
        secondNum = Character.getNumericValue(input.charAt(1));
        thirdNum = Character.getNumericValue(input.charAt(2));

        switch (nums) {
            case 1:
                sumNums = firstNum + secondNum + thirdNum;
                sumNums2 = sumNums;
            case 2:
                sumNums = firstNum + secondNum * thirdNum;
                if (sumNums > sumNums2) {
                    sumNums2 = sumNums;
                }
            case 3:
                sumNums = firstNum * secondNum + thirdNum;
                if (sumNums > sumNums2) {
                    sumNums2 = sumNums;
                }
            case 4:
                sumNums = firstNum * thirdNum + secondNum;
                if (sumNums > sumNums2) {
                    sumNums2 = sumNums;
                }
            case 5:
                sumNums = firstNum * secondNum * thirdNum;
                if (sumNums > sumNums2) {
                    sumNums2 = sumNums;
                    break;
                }
        }
        System.out.println(sumNums2);
    }
}

提前感谢!

java loops if-statement switch-statement switching
3个回答
0
投票

问题是您要打开变量nums,您希望该变量为3位数字。输入的任何3位数字与您的任何一位数字case语句都不匹配,因此一起掉入了开关。

看来您打算让用户首先输入1到5的歌手数字,代表一种匹配其中一种情况的操作-您从不这样做。

此外,在扫描输入之前,您应该先通过System.out.println()语句打印出您要输入的内容。


0
投票

代码看起来很奇怪:

switch (nums) {  // 111, 257  wrong input, no match

// suggest to add default, so you can debug the problem
}

0
投票

使用if语句修复:

import java.util.Scanner;

public class Game {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int nums = Integer.parseInt(input);
        int firstNum, secondNum, thirdNum, sumNums, sumNums2;

        firstNum = Character.getNumericValue(input.charAt(0));
        secondNum = Character.getNumericValue(input.charAt(1));
        thirdNum = Character.getNumericValue(input.charAt(2));

        sumNums2 = firstNum + secondNum + thirdNum;
        sumNums = firstNum + secondNum * thirdNum;
        if (sumNums > sumNums2) {
            sumNums2 = sumNums;
        }
        sumNums = firstNum * secondNum + thirdNum;
        if (sumNums > sumNums2) {
            sumNums2 = sumNums;
        }
        sumNums = firstNum * thirdNum + secondNum;
        if (sumNums > sumNums2) {
            sumNums2 = sumNums;
        }
        sumNums = firstNum * secondNum * thirdNum;
        if (sumNums > sumNums2) {
            sumNums2 = sumNums;

        }
        System.out.println(sumNums2);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.