我对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