while循环在错误点开始[Java]
我对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
注:这不是一个答案,只是对代码的结构的批评
您的输入方式“是”和“否”是容易出错,因为你是在重复使用相同的代码并重新开始。制作一个返回布尔值的方法。例如:
/**
* 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;
}
这也允许输入系统更坚固,因为它们给出了多个镜头输入一个响应,并且它们可以输入完整的乱码而不会破坏代码。您也可以创建一个类似的系统来检索数字。
Rubet23这是我在我的回答评论中指的那种东西。 –
你忘了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(),使每一次大写输入。通过这种方式,您不必为两种情况编写相同的代码两次;你可以测试该字母的大写实例。
祝你好运!
我相信我的解决方案会起作用;然而,我确实建议您接受上述评论者的建议,并花一些时间清理您的代码并删除不必要的行,然后再继续您的项目。它会让你更容易,你会成为一个更好的程序员。 :) –
你有两个运行while(aga == 'y' || aga == 'Y');
的循环。两者之间没有区别。如果你想要两种可能性以两种不同的方式行事,你需要以两种不同的方式使用它们。
这似乎像它应该表现得像你想让它当您移动比较其他while
条件之一:
while(aw > 1 && aw <329108321 && aga != 'y');
请注意,你的代码是非常难以阅读,因为你的缩进版本不一致。
你是什么意思“不工作”? *不是debuger的替代品 –
很难在代码中发现错误,因为它对于重复的情况非常困惑,而且很难阅读。您应该重写代码,而不是在尝试查找错误之前,应该先对其进行规划。如果你想,除了那个之外,我可以帮忙,但我做不了多少。 – tomtzook
创建一个新项目并将所有代码从do while循环中取出。在while while循环询问问题并选择一个选项结束时。如果它工作正常,开始添加你的其他代码。 – Sedrick