while 循环从错误的点开始 [Java]
Do while loop starts on wrong point [Java]
我对 java 很陌生,对 while 循环也很陌生。
我的循环有问题,我制作了一个基本的计算器,并在你计算了一些东西之后给了它一个计算其他东西的选项。当出现此选项时,我输入 "Y"(大写)它 "restarts",但如果我回答 "y"(小写),它会返回到您选择的内容。
例如:如果我启动计算器,我会得到 5 个选项,如果我选择选项 1 并走到最后,我会得到一个选择:如果我想 "try again" 那么如果我选择回答 "Y" 整个事情可以追溯到 "screen" ,在那里我可以选择要做什么。但是当我选择回答 "y" 时,它又回到了我之前选择的那个点。就像我选择选项 1 并回答 "y" 它会回到选项 1.
我试图在网上查找但找不到任何东西,所以要么我不知道使用正确的关键字,要么其他人不经常 运行 解决我的问题。
这是计算器,里面有一些荷兰语单词,比如计算器或非常糟糕的英语,但 none 无论如何都很重要:
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
您在检查小写字母时忘记将 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');
请注意,您的代码很难阅读,因为您的缩进非常不一致。
注:这不是回答,只是对代码结构的批评
您输入 "yes" 和 "no" 的方式很容易出错,因为您一遍又一遍地重复使用相同的代码。创建一个 returns 布尔值的方法。例如:
/**
* 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;
}
这也让输入系统更加健壮,因为他们可以多次输入响应,并且他们可以在不破坏代码的情况下输入完全乱码。你也可以做一个类似的系统来检索号码。
我对 java 很陌生,对 while 循环也很陌生。 我的循环有问题,我制作了一个基本的计算器,并在你计算了一些东西之后给了它一个计算其他东西的选项。当出现此选项时,我输入 "Y"(大写)它 "restarts",但如果我回答 "y"(小写),它会返回到您选择的内容。
例如:如果我启动计算器,我会得到 5 个选项,如果我选择选项 1 并走到最后,我会得到一个选择:如果我想 "try again" 那么如果我选择回答 "Y" 整个事情可以追溯到 "screen" ,在那里我可以选择要做什么。但是当我选择回答 "y" 时,它又回到了我之前选择的那个点。就像我选择选项 1 并回答 "y" 它会回到选项 1.
我试图在网上查找但找不到任何东西,所以要么我不知道使用正确的关键字,要么其他人不经常 运行 解决我的问题。
这是计算器,里面有一些荷兰语单词,比如计算器或非常糟糕的英语,但 none 无论如何都很重要:
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
您在检查小写字母时忘记将 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');
请注意,您的代码很难阅读,因为您的缩进非常不一致。
注:这不是回答,只是对代码结构的批评
您输入 "yes" 和 "no" 的方式很容易出错,因为您一遍又一遍地重复使用相同的代码。创建一个 returns 布尔值的方法。例如:
/**
* 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;
}
这也让输入系统更加健壮,因为他们可以多次输入响应,并且他们可以在不破坏代码的情况下输入完全乱码。你也可以做一个类似的系统来检索号码。