简体   繁体   English

在do-while循环Java中将变量重置为零

[英]Reset variables back to zero in do-while loop Java

I am working on an example using a do-while loop and switch statement. 我正在做一个使用do-while循环和switch语句的示例。 What I basically need is to accumulate numbers and depending on user input either add, substract, multiply or divide (mini calculator type). 我基本上需要的是累加数字,并根据用户输入加,减,乘或除(迷你计算器类型)。

The problem is when I ask the user to go back to the main menu the program does not reset the value as it is before the loop. 问题是当我要求用户返回主菜单时,程序不会像循环前一样重置该值。 The result is always the previous result. 结果始终是先前的结果。

Here is the code, it will explain it better. 这是代码,它将更好地解释它。

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");

}
}

A couple things are going on here. 这里发生了几件事。 To answer your question concisely, it's because you didn't reassign your variables before re-looping. 为了简洁地回答您的问题,是因为您没有在重新循环之前重新分配变量。 Since you don't reassign result and quit, quit is false so it closes the loop, and result is unchanged so it then prints the same result. 由于您不重新分配结果并退出,因此quit为false会关闭循环,而result不变,因此它会输出相同的结果。 Try this: 尝试这个:

 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;

I think it's the most straight-forward solution to your problem. 我认为这是解决您问题的最直接的方法。

EDIT: I also wanted to add that using quit as the while condition seems a little counter-intuitive. 编辑:我还想补充一点,使用quit作为while条件似乎有点违反直觉。 If I saw a condition quit that was true, my assumption would be that it would break the loop, not continue it. 如果我看到退出的条件是真的,我的假设是它将打破循环,而不是继续下去。 You might make your code a bit clearer by designating more meaningful variable names. 您可以通过指定更有意义的变量名称来使代码更清晰。 So instead of saying something like: 因此,与其说类似的话:

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

This may be a little clearer: 这可能更清楚一些:

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

Just a general suggestion. 只是一般性建议。

You can try to call main() again, but I'm not sure if it will work, solution can be make your own method eg. 您可以尝试再次调用main() ,但是不确定是否可以使用,解决方案可以使用您自己的方法,例如。 init() - where you will set vars into init state, and eg. init() -您将在其中将var设置为init状态,例如 work() , what will be remaining code :D work() ,剩下的代码是什么:D

EDIT: you can make it this way 编辑:你可以这样

      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");
                }

            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM