我對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
你是什麼意思「不工作」? StackOverflow不是debuger的替代品 –
很難在代碼中發現錯誤,因爲它對於重複的情況非常困惑,而且很難閱讀。您應該重寫代碼,而不是在嘗試查找錯誤之前,應該先對其進行規劃。如果你想,除了那個之外,我可以幫忙,但我做不了多少。 – tomtzook
創建一個新項目並將所有代碼從do while循環中取出。在while while循環詢問問題並選擇一個選項結束時。如果它工作正常,開始添加你的其他代碼。 – Sedrick