【问题标题】:Java skipping third case statementJava跳过第三个case语句
【发布时间】:2017-04-05 09:20:44
【问题描述】:

这是一个简单的计算器,我需要使用它来使用基本命令。这个项目的目标是编写异常(非常容易),但是,我一辈子都想不通。我到处找。

无论是 if/else 语句还是 Switch/Case 语句,总是会跳过第三条语句。当用户输入“m”时,它应该将计算的值保存到一个占位符变量中以便能够被调用(同样,超级简单)。我在添加部分添加了一个默认案例语句,并将我的保存方法添加到默认语句中,它运行良好。其他所有命令(r 表示调用,c 表示清除,e 表示退出)也很有效。用于保存的 m 根本没有.....

import java.util.Scanner;

public class Calculator {
public static Scanner input = new Scanner(System.in);
public static double placeHolder;

public static void clear() {
    System.out.println("Save has been deleted, Screen has been cleared.");
}

public static void end() {
    System.out.println("Program ended..");
    input.close();
    System.exit(0);
}

public static void save(double initValue) {
    System.out.println("Number saved!");
    placeHolder = initValue;
}

public static void recall() {
    if (placeHolder != 0){
        System.out.println("Memory Place Holder Set To: " + placeHolder);
    }
    else {
        System.out.println("There is no data saved.");
    }
}

public static void commands() {
    System.out.println("e = end | c = clear | m = save | r = recall | o = continue");
    String command = input.nextLine();
    if (command.equals("e")){
        end();
    }
    else if (command.equals("c")){
        clear();
    }
    else if (command.equals("r")){
        recall();
    }

}




public static void main(String[] args) {
    boolean loop = true;
    while (loop == true){
        commands();
        System.out.println("Please enter what you would like to do: (+,-,*,/,%)");
        String function = input.nextLine();
        System.out.println("Enter the first number to be calucalted (If dividing, this is the numerator):");
        double n1 = input.nextDouble();
        System.out.println("Enter the second number to be calucalted (If dividing, this is the denominator):");
        double n2 = input.nextDouble();

        //=======================
        // Addition
        //=======================
        if (function.equals("+")){
            double sum = n1+n2;
            System.out.println(n1+"+"+ n2 +" = " + sum);
            System.out.println("e = end | c = clear | m = save | r = recall");
            String command = input.nextLine();
            input.nextLine();
            switch (command){
            case "e":
                end();
                break;
            case "c":
                clear();
                break;
            case "m":
                save(sum);
                break;
            case "r":
                recall();
                break;
            default: 
                System.out.println("Default");
                save(sum);
                break;
            }

        }
        //=======================
        // Subtraction
        //=======================
        else if (function.equals("-")){
            double sum = n1-n2;
            System.out.println(n1 + "-" + n2 +" = " + sum);
            System.out.println("e = end | c = clear | m = save | r = recall");
            String command = input.nextLine();
            input.nextLine();
            switch (command){
            case "e":
                end();
            case "c":
                clear();
            case "m":
                save(sum);
            case "r":
                recall();
            }
        }
        //=======================
        // Multiplication
        //=======================
        else if (function.equals("*")){
            double sum = n1*n2;
            System.out.println(n1 + "*" + n2 +" = " + sum);
            System.out.println("e = end | c = clear | m = save | r = recall");
            String command = input.nextLine();
            input.nextLine();
            switch (command){
            case "e":
                end();
            case "c":
                clear();
            case "m":
                save(sum);
            case "r":
                recall();
            }
        }
        //=======================
        // Division
        //=======================
        else if (function.equals("/")){
            double sum = n1/n2;
            System.out.println(n1 + "/" + n2 +" = " + sum);
            System.out.println("e = end | c = clear | m = save | r = recall");
            String command = input.nextLine();
            input.nextLine();
            switch (command){
            case "e":
                end();
            case "c":
                clear();
            case "m":
                save(sum);
            case "r":
                recall();
            }
        }
        //=======================
        // Mod
        //=======================
        else if (function.equals("%")){
            double sum = n1%n2;
            System.out.println(n1 + "%" + n2 +" = " + sum);
            System.out.println("e = end | c = clear | m = save | r = recall");
            String command = input.nextLine();
            input.nextLine();
            switch (command){
            case "e":
                end();
            case "c":
                clear();
            case "m":
                save(sum);
            case "r":
                recall();
            }
        }
    }   

        //=======================
        // Dictate loop duration:
        //=======================
        System.out.println("Would you like to continue? (Y|N): ");
        String ans = input.nextLine();
        if (ans.equals("N") || ans.equals("n")){
            System.out.println("Closing Program");
            loop = false;
            end();
        }
    }

}

有问题的主要代码是这样的:我知道其余的没有默认或中断语句。这个确实如此,我正在调试并试图找出 m 失败的原因。现在如果你是他 m,它只是进入默认的 case 语句,并不能解决问题。

 switch (command){
        case "e":
            end();
            break;
        case "c":
            clear();
            break;
        case "m":
            save(sum);
            break;
        case "r":
            recall();
            break;
        default: 
            System.out.println("Default");
            save(sum);
            break;

================================================ =========================== 这是事后查看此帖子的任何人的修复:

public static void main(String[] args) {
    boolean loop = true;
    while (loop == true){
        commands();
        System.out.println("Please enter what you would like to do: (+,-,*,/,%)");
        String function = input.nextLine();
        System.out.println("Enter the first number to be calucalted (If dividing, this is the numerator):");
        double n1 = input.nextDouble();
        input.nextLine();
        System.out.println("Enter the second number to be calucalted (If dividing, this is the denominator):");
        double n2 = input.nextDouble();
        input.nextLine();

        //=======================
        // Addition
        //=======================
        if (function.equals("+")){
            double sum = n1+n2;
            System.out.println(n1+"+"+ n2 +" = " + sum);
            System.out.println("e = end | c = clear | m = save | r = recall");
            String command = input.nextLine();
            switch (command){
            case "e":
                end();
                break;
            case "c":
                clear();
                break;
            case "m":
                save(sum);
                break;
            case "r":
                recall();
                break;
            }

        }

【问题讨论】:

  • 你能缩小焦点吗?你在哪里看到问题?这是很多代码,commands() 中有一个 3 部分的 if-else 和 main() 中的几个多部分 switch 语句,一些有中断,一些没有,一些有默认值,一些没有。
  • 您所有的switch 语句,除了第一个语句,缺少break 语句
  • 我复制并粘贴了您的代码,它起作用了,当我为 sum 添加数字时,它会给出结果,如果我按 m,它会显示数字已保存! ?
  • 不确定你的问题是什么,我复制了代码并且一切正常(除了那些缺少的 break 语句,但即便如此 sum 应该一直有效)
  • 你应该也可以将所有不同的操作提取到一个方法中,这样你就没有那么多重复的代码。

标签: java if-statement switch-statement


【解决方案1】:
  1. 在每个input.nextDouble(); 之后,您需要调用input.nextLine(); 来消耗新行,请参阅Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
  2. 在每个String command = input.nextLine(); 之后删除不必要的input.nextLine();

【讨论】:

  • 是的,做到了。为什么 nextDouble();去做?或者究竟发生了什么让这一切发生?太烦人了!
  • @JoshuaFaust 如提供的链接中所述,nextDouble 不使用换行符(当您按 Enter 时),因此当您尝试阅读 m 时,它实际上是在阅读之前未使用的剩余换行符。
【解决方案2】:

nextDouble() 不使用新行。您可以通过解析整行来解决此问题,然后使用Double.parseDouble() 从这样的行中获取双精度值

....
    System.out.println("Enter the first number to be"+ 
          "calculated (If dividing, this is the numerator):");

    double n1 = Double.parseDouble(input.nextLine());

    System.out.println("Enter the second number to be" + 
          "calculated (If dividing, this is the denominator):");

    double n2 = Double.parseDouble(input.nextLine());

    if (function.equals("+")) {
     double sum = n1 + n2;
     System.out.println(n1 + "+" + n2 + " = " + sum);
     System.out.println("e = end | c = clear | m = save | r = recall");
     String command = input.nextLine();

     switch (command) {
     case "e":
      end();
      break;
      case "c":
      clear();
      break;
     case "m":
      save(sum);
      break;
     case "r":
      recall();
      break;
     }
....

注意

您不必使用布尔变量检查 while(loop == true),只需通过 while(loop) 进行检查即可。

【讨论】:

    【解决方案3】:

    查看Complete code link

    您需要注意的事项很少。

    1. 具有良好的异常处理能力。
    2. 验证用户输入

    我对您的代码所做的更改-

    1. 清除clear()方法中的placeHolder
    2. 使用input.next() 代替input.nextLine() 并删除额外的input.newLine() 方法
    3. 在所有 switch 语句中添加了 break 语句

    您需要将代码中的很多东西模块化。看看Best Coding实践

    【讨论】:

      【解决方案4】:

      input.nextDouble() 读取数字,但对行尾不执行任何操作。因此,当您稍后在代码中执行此操作时:

      String command = input.nextLine();
      

      它处理剩余的行尾,而不是您接下来键入的“m”。

      要解决这个问题,请做一些处理输入的行尾,例如在调用 nextDouble() 后添加 input.nextLine():

      double n1 = input.nextDouble();
      input.nextLine();
      double n2 = input.nextDouble();
      input.nextLine();
      

      【讨论】:

        猜你喜欢
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 2017-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-24
        相关资源
        最近更新 更多