2017-07-26 53 views
0

我對Java很新,在循環中做的很新。 我有一個循環的問題,我做了一個基本的計算器,並給了它一個選項來計算一些事情後,你計算了一些東西。當這個選項來了,我輸入「Y」(大寫)它「重新啓動」,但如果我回答「Y」(小寫)它會回到你選擇的。例如:如果我啓動計算器,我會得到5個選項,如果我選擇選項編號1,並且到最後,我會得到一個選擇:如果我想「再試一次」,那麼如果我選擇回答「是」,整個事情回到「屏幕」,我可以選擇做什麼。但是當我選擇回答「y」時,它會回到我之前選擇的位置。如果我選擇選項1,我回答「是」它回到選項1.while循環在錯誤點開始[Java]

我試圖在網上查找它,但找不到任何東西,所以要麼我不知道使用正確的關鍵字或其他人做不經常遇到我的問題。

這裏是計算器,也有它的幾個荷蘭的話就像計算器或非常糟糕的英語,但他們沒有以任何方式無關緊要:

import java.util.*; 

public class GEKENMCHINENENE { 
    public static void main(String[] args){ 

     char aga = 'y'; 
     while(aga == 'y' || aga == 'Y'){ 
     Scanner antwoord = new Scanner(System.in); 
     int aw; 

do{ 

    System.out.println("enter 1 for addition. enter 2 for subtraction. enter 3 for division. enter 4 for multiplication."); 
    System.out.println("+------------------------------+"); 
    System.out.println("|   Calculator   |"); 
    System.out.println("|------------------------------|"); 
    System.out.println("| Press 1 for addition  |"); 
    System.out.println("| Press 2 for subtraction  |"); 
    System.out.println("| Press 3 for division  |"); 
    System.out.println("| Press 4 for multiplication |"); 
    System.out.println("| Press 5 for my social media |"); 
    System.out.println("| *credit to creepershelp* |"); 
    System.out.println("|  *on pastebin*   |"); 
    System.out.println("+------------------------------+"); 


    aw = antwoord.nextInt(); 

    do{ 

     if (aw > 5){ 
      System.out.println("That number is out of range, try aga within the range of 1 - 4"); 
     aw = antwoord.nextInt(); 

     }else if(aw < 1){ 
      System.out.println("That number is out of range, try aga within the range of 1 - 4"); 
     aw = antwoord.nextInt(); 
     }else if(aw < -1){ 
      System.out.println("That number is out of range, try aga within the range of 1 - 4"); 
     aw = antwoord.nextInt(); 
     } 

     if(aw == 1){ 
      System.out.println("You choose for 1, addition. Enter your first number."); 
     double fnum = antwoord.nextDouble(); 
      System.out.println("Enter your second number"); 
     double snum = antwoord.nextDouble(); 
     double awp = fnum + snum; 
      System.out.println("The answer is " + awp); 
      System.out.println("Wood you like to reken uit something else? [Y/N]"); 
      aga = antwoord.next().charAt(0); 


      if(aga == 'n'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'n'){ 
       aga = 'n'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'N'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'N'){ 
       aga = 'N'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'y'){ 
       System.out.println("Please wait!"); 
      }else if(aga == 'Y'){ 
       System.out.println("Please wait!"); 
      aw = 0; 
      } 
     } 

     if(aw == 2){ 
      System.out.println("You choose for 2, subtraction. Enter your first number."); 
     double fnum = antwoord.nextDouble(); 
      System.out.println("Enter your second number"); 
     double snum = antwoord.nextDouble(); 
     double awp = fnum - snum; 
      System.out.println("The answer is " + awp); 
      System.out.println("Wood you like to reken uit something else? [Y/N]"); 
      aga = antwoord.next().charAt(0); 


      if(aga == 'n'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'n'){ 
       aga = 'n'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'N'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'N'){ 
       aga = 'N'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'y'){ 
       System.out.println("Please wait!"); 
      }else if(aga == 'Y'){ 
       System.out.println("Please wait!"); 
      aw = 0; 
      } 
     } 

     if(aw == 3){ 
      System.out.println("You choose for 3, division. Enter your first number."); 
     double fnum = antwoord.nextDouble(); 
      System.out.println("Enter your second number"); 
     double snum = antwoord.nextDouble(); 
     double awp = fnum/snum; 
      System.out.println("The answer is " + awp); 
      System.out.println("Wood you like to reken uit something else? [Y/N]"); 
      aga = antwoord.next().charAt(0); 


      if(aga == 'n'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'n'){ 
       aga = 'n'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'N'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'N'){ 
       aga = 'N'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'y'){ 
       System.out.println("Please wait!"); 
      }else if(aga == 'Y'){ 
       System.out.println("Please wait!"); 
      aw = 0; 
      } 
     } 

     if(aw == 4){ 
      System.out.println("You choose for 4, multiplication. Enter your first number."); 
     double fnum = antwoord.nextDouble(); 
      System.out.println("Enter your second number"); 
     double snum = antwoord.nextDouble(); 
     double awp = fnum * snum; 
      System.out.println("The answer is " + awp); 
      System.out.println("Wood you like to reken uit something else? [Y/N]"); 
      aga = antwoord.next().charAt(0); 


      if(aga == 'n'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'n'){ 
       aga = 'n'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'N'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'N'){ 
       aga = 'N'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'y'){ 
       System.out.println("Please wait!"); 
      }else if(aga == 'Y'){ 
       System.out.println("Please wait!"); 
      aw = 0; 
      } 

     }if(aw == 5){ 
      System.out.println("Instagram: rubettt, https://www.instagram.com/rubettt/?hl=en"); 
      System.out.println("Snapchat: rubet23-1212121"); 
      System.out.println("Wood you like to reken uit something else? [Y/N]"); 
      aga = antwoord.next().charAt(0); 


      if(aga == 'n'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'n'){ 
       aga = 'n'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'N'){ 
       System.out.println("Shutting down..."); 
      }if(aga == 'N'){ 
       aga = 'N'; 
       antwoord.close(); 
       break; 
      }else if(aga == 'y'){ 
       System.out.println("Please wait!"); 
      }else if(aga == 'Y'){ 
       System.out.println("Please wait!"); 
      aw = 0; 
      } 
     }if(aw == 0){ 
      System.out.println("Succesfully shut down!"); 
     }else if(aga == 'n' || aga == 'N'){ 
      System.out.println("Succesfully shut down!"); 
     } 

     }while(aw > 1 && aw <329108321); 

     }while(aga == 'y' || aga == 'Y'); 


     } 
    } 
} 

我試圖把它改變「Y」到「 Y「,但這也沒有奏效。 如果有人有任何想法,請幫助。我很想知道這是爲什麼,以及我如何解決它。在此先感謝,

-Rubet23

+2

你是什麼意思「不工作」? StackOverflow不是debuger的替代品 –

+0

很難在代碼中發現錯誤,因爲它對於重複的情況非常困惑,而且很難閱讀。您應該重寫代碼,而不是在嘗試查找錯誤之前,應該先對其進行規劃。如果你想,除了那個之外,我可以幫忙,但我做不了多少。 – tomtzook

+0

創建一個新項目並將所有代碼從do while循環中取出。在while while循環詢問問題並選擇一個選項結束時。如果它工作正常,開始添加你的其他代碼。 – Sedrick

回答

0

注:這不是一個答案,只是對代碼的結構的批評

您的輸入方式「是」和「否」是容易出錯,因爲你是在重複使用相同的代碼並重新開始。製作一個返回布爾值的方法。例如:

/** 
* This method will return true for yes and false for no. 
* It is private, because it is unnecessary for other 
* Classes to use it. 
*/ 
private boolean sayYes(String text){ 
    Scanner keyboard = new Scanner(System.in); 
    while(true){ 
     System.out.print(text); 
     // Gets keyboard input, trims it, and makes it lower case. 
     String input = keyboard.nextLine().trim().toLowerCase; 
     // Checks for "Yes" 
     if(input.equals("y") || input.equals("yes")) 
      return true; 
     // Checks for "No" 
     if(input.equals("n") || input.equals("no")) 
      return false; 
     // If we got here, then it was neither a "yes" or "no", 
     // so we complain, and go around the loop again 
     System.out.println("Invalid input."); 
    } 
    // The keyboard Scanner is not closed so the program can 
    // continue to receive keyboard input from other places. 
} 

然後,可以調用該方法具有:

if(sayYes("Wood you like to reken uit something else? [Y/N]\n")){ 
    aw = 0; 
} else { 
    antwoord.close(); 
    break; 
} 

這也允許輸入系統更堅固,因爲它們給出了多個鏡頭輸入一個響應,並且它們可以輸入完整的亂碼而不會破壞代碼。您也可以創建一個類似的系統來檢索數字。

+0

Rubet23這是我在我的回答評論中指的那種東西。 –

0

你忘了AW設置爲0時您檢查小寫「Y」,因此while循環重複,因爲你從來沒有改變多少。在最底層,你的代碼應該說:

//The stuff above this 
else if(aga == 'y'){ 
    System.out.println("Please wait!"); 
    aw = 0; 
} 
//The rest of your code` 

這並不一定與你的問題的事,但我認爲你應該使用aga.toUpper(),使每一次大寫輸入。通過這種方式,您不必爲兩種情況編寫相同的代碼兩次;你可以測試該字母的大寫實例。

祝你好運!

+0

我相信我的解決方案會起作用;然而,我確實建議您接受上述評論者的建議,並花一些時間清理您的代碼並刪除不必要的行,然後再繼續您的項目。它會讓你更容易,你會成爲一個更好的程序員。 :) –

0

你有兩個運行while(aga == 'y' || aga == 'Y');的循環。兩者之間沒有區別。如果你想要兩種可能性以兩種不同的方式行事,你需要以兩種不同的方式使用它們。

這似乎像它應該表現得像你想讓它當您移動比較其他while條件之一:

while(aw > 1 && aw <329108321 && aga != 'y'); 

請注意,你的代碼是非常難以閱讀,因爲你的縮進版本不一致。