簡體   English   中英

在do-while循環Java中將變量重置為零

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

我正在做一個使用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");

}
}

這里發生了幾件事。 為了簡潔地回答您的問題,是因為您沒有在重新循環之前重新分配變量。 由於您不重新分配結果並退出,因此quit為false會關閉循環,而result不變,因此它會輸出相同的結果。 嘗試這個:

 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;

我認為這是解決您問題的最直接的方法。

編輯:我還想補充一點,使用quit作為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
     }
 }

只是一般性建議。

您可以嘗試再次調用main() ,但是不確定是否可以使用,解決方案可以使用您自己的方法,例如。 init() -您將在其中將var設置為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");
                }

            }

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM