【问题标题】:Reset variables back to zero in do-while loop Java在do-while循环Java中将变量重置为零
【发布时间】:2015-08-16 17:03:24
【问题描述】:

我正在研究一个使用 do-while 循环和 switch 语句的示例。我基本上需要的是累积数字,并根据用户输入加、减、乘或除(迷你计算器类型)。

问题是当我要求用户返回主菜单时,程序不会像循环之前那样重置值。结果始终是前一个结果。

这是代码,它会更好地解释它。

import java.util.Scanner;
public class SwitchLoopNumbers{
public static void main(String[] args){

  Scanner scan = new Scanner(System.in);
  int numbers=0;
  int result=0;
  int option;
  boolean quit = true;
  String done="";
  do{
     System.out.println("CALCULATOR MENU");
     System.out.println("********** ****");
     System.out.println("\n1. Add");
     System.out.println("2. Substract");
     System.out.println("3. Multiply");
     System.out.println("4. Divide");

     System.out.println();
     System.out.print("Enter your option >> ");
     option = scan.nextInt();

     while(quit){

        switch(option){

           case 1: 

              System.out.print("Enter numbers, type 0 when done >> ");
              numbers = scan.nextInt();
              if(numbers==0)
              quit=false;
              result +=numbers;
              break;




           case 2:
              System.out.print("Enter numbers, type 0 when done >> ");
              numbers = scan.nextInt();
              result -=numbers;
              if(numbers==0)
              quit=false;
              break;

        }

     }





     System.out.println("The total is: "+result);
     System.out.println("Back to main menu ? y/n ");
     scan.nextLine();
     done = scan.nextLine(); 
     //I did reset numbers and result here to zero but it did not work
  }



  while("y".equalsIgnoreCase(done)); 
  System.out.println("Thank you for using calculator");

}
}

【问题讨论】:

    标签: java loops switch-statement reset


    【解决方案1】:

    这里发生了几件事。简明扼要地回答您的问题,这是因为您在重新循环之前没有重新分配变量。由于您没有重新分配结果并退出,因此退出为假,因此它关闭了循环,并且结果保持不变,因此它会打印相同的结果。试试这个:

     System.out.println("The total is: "+result);
     System.out.println("Back to main menu ? y/n ");
     scan.nextLine();
     done = scan.nextLine(); 
     numbers = 0;
     result = 0;
     quit = true;
    

    我认为这是解决您的问题的最直接的解决方案。

    编辑:我还想补充一点,使用退出作为 while 条件似乎有点违反直觉。如果我看到一个条件退出是真的,我的假设是它会打破循环,而不是继续它。通过指定更有意义的变量名,您可以使您的代码更清晰一些。所以不要说这样的话:

     boolean quit = true;
     while(quit) {
         //do stuff
         if (some_condition) {
             quit = false;
             //close loop
         }
     }
    

    这可能会更清楚一点:

    boolean quit = false;
     while(!quit) {
         //do stuff
         if (some_condition) {
             quit = true;
             //close loop
         }
     }
    

    只是一般建议。

    【讨论】:

    • 我明白了。我之前所做的是我重置数字并得到与您的答案完全相同的结果,但我没有将循环重置为真。现在它是有道理的,像它应该的那样工作。感谢您的帮助。
    【解决方案2】:

    您可以尝试再次致电main(),但我不确定它是否会起作用,解决方案可以是您自己的方法,例如。 init() - 您将在其中将 vars 设置为 init 状态,例如。 work(),剩下的代码是什么:D

    编辑:你可以这样做

          import java.util.Scanner;
    
            public class main {
        //if you want work with result after user will write "y" in the end
                static int result = 0;
    
                public static void main(String[] args) {
                    Scanner scan = new Scanner(System.in);
                    int numbers = 0;
    
                    int option;
                    boolean quit = false;
                    String done = "";
        //int result = 0; // if you want also init result 
    
    
    
                        // menu
                        System.out.println("CALCULATOR MENU");
                        System.out.println("********** ****\n");
                        System.out.println("1. Add");
                        System.out.println("2. Substract");
                        System.out.println("3. Multiply");
                        System.out.println("4. Divide");
    
                        // user menu input read
                        System.out.println();
                        System.out.print("Enter your option >> ");
                        option = scan.nextInt();
    
                        switch (option) {
                        case 1:
                            while (!quit) {
                                System.out.print("Enter numbers, type 0 when done >> ");
                                numbers = scan.nextInt();
                                if (numbers == 0) {
                                    quit = true;
                                }
                                result += numbers; // result = result + numbers
                            }
    
                            break;
                        case 2:
                            while (!quit) {
                                System.out.print("Enter numbers, type 0 when done >> ");
                                numbers = scan.nextInt();
                                result -= numbers; // result = result - numbers
                                if (numbers == 0) {
                                    quit = true;
                                }
                            }
                            break;
                        default:
                                System.out.println("Bad inpout");
                            break;
                        }
    
    
                    System.out.println("The total is: " + result);
                    System.out.println("Back to main menu ? y/n ");
                    scan.nextLine();
                    done = scan.nextLine();
    
    //recursive call - run main() again 
                    if (done.equals("y")) {
                        main(args);
                    } else {
                        System.out.println("Thank you for using calculator");
                    }
    
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-16
      • 2021-12-26
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2018-02-15
      • 1970-01-01
      相关资源
      最近更新 更多