Do-While 语句陷入无限循环
Do-While statement stuck in infinite loop
我已经盯着这个看了好几个小时了,我一辈子都弄不明白为什么 运行 时的输出卡在一个循环中,就像这样(是的,我现在有只需更正估计的拼写):
你的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸。
您的未来 child 估计会长到 4 英尺 10 英寸
每次我写循环时都会发生这种情况。
//Allows keyboard to be used
Scanner keyboardInput = new Scanner(System.in);
//Allows user to input numbers
System.out.println("Enter the gender of your future child. Use 1 for Female and 0 for Male: ");
int Gender = keyboardInput.nextInt();
System.out.println("Enter the height in feet, then in inches of the mom: ");
int MomHeight = keyboardInput.nextInt();
System.out.println("Enter the height in feet, then the height in inches of the dad: ");
int DadHeight = keyboardInput.nextInt();
int female;
int male;
int HeightFeet;
int HeightInches;
DecimalFormat feet = new DecimalFormat("#0");
DecimalFormat inches = new DecimalFormat("#0");
//Loop statements
while (Gender == 0)
{
male = (MomHeight * 13 / 12 + DadHeight) / 2;
HeightFeet = male / 12;
HeightInches = male % 12;
System.out.print("Your future child is estimated to grow to " + feet.format(HeightFeet));
System.out.print(" feet and " + inches.format(HeightInches));
System.out.print(" inches.");
System.out.println("");
}
while (Gender == 1)
{
female = (DadHeight * 12 /13 + MomHeight) /2;
HeightFeet = female / 12;
HeightInches= female % 12;
System.out.print("Your future child is estmated to grow to " + feet.format(HeightFeet));
System.out.print(" feet and " + inches.format(HeightInches));
System.out.print(" inches.");
System.out.println("");
} } }
在您的循环中,Gender
永远不会被修改。所以你确实永远循环。
现在,我认为您不需要 while
声明。
if else if
语句会更可取,因为您不会从用户那里获取新的输入来循环,但您想根据特定条件(男性或女性)应用处理。
顺便说一句,您应该将变量命名为 gender
而不是 Gender
以尊重 Java 命名约定:
if (gender == 0){
...
}
else if (gender == 1){
...
}
如果你想多次重复所有的处理,你可以使用一个循环遍历:
boolean again = false;
do{
if (gender == 0){
...
}
else if (gender == 1){
...
}
...
again = keyboardInput.nextBoolean();
} while (again);
为了退出循环,您需要在循环执行所需次数后条件变为假。
同样在上面的代码中,您似乎是在要执行的语句之间做出选择,而不是 运行 传统的循环。考虑使用 "if" 语句
while (Gender == 0){
//Do this
}
while (Gender == 1){
//Do this instead
}
如果您希望循环在打印输出语句 "X" 次后退出,您宁愿将条件基于计数变量。
所以做出选择是基于性别变量,打印语句是基于计数变量。
//Assume print is to be done 2 times
int count = 1;
if(Gender == 0){
//Female selection
while( count < 3 ){
// Execute female code
count++;
}
else if(Gender == 1){
//Male selection
while( count < 3 ){
// Execute male code
count++;
}
我已经盯着这个看了好几个小时了,我一辈子都弄不明白为什么 运行 时的输出卡在一个循环中,就像这样(是的,我现在有只需更正估计的拼写):
你的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸。 您的未来 child 估计会长到 4 英尺 10 英寸
每次我写循环时都会发生这种情况。
//Allows keyboard to be used Scanner keyboardInput = new Scanner(System.in);
//Allows user to input numbers System.out.println("Enter the gender of your future child. Use 1 for Female and 0 for Male: "); int Gender = keyboardInput.nextInt(); System.out.println("Enter the height in feet, then in inches of the mom: "); int MomHeight = keyboardInput.nextInt(); System.out.println("Enter the height in feet, then the height in inches of the dad: "); int DadHeight = keyboardInput.nextInt(); int female; int male; int HeightFeet; int HeightInches; DecimalFormat feet = new DecimalFormat("#0"); DecimalFormat inches = new DecimalFormat("#0"); //Loop statements while (Gender == 0) { male = (MomHeight * 13 / 12 + DadHeight) / 2; HeightFeet = male / 12; HeightInches = male % 12; System.out.print("Your future child is estimated to grow to " + feet.format(HeightFeet)); System.out.print(" feet and " + inches.format(HeightInches)); System.out.print(" inches."); System.out.println(""); } while (Gender == 1) { female = (DadHeight * 12 /13 + MomHeight) /2; HeightFeet = female / 12; HeightInches= female % 12; System.out.print("Your future child is estmated to grow to " + feet.format(HeightFeet)); System.out.print(" feet and " + inches.format(HeightInches)); System.out.print(" inches."); System.out.println(""); } } }
在您的循环中,Gender
永远不会被修改。所以你确实永远循环。
现在,我认为您不需要 while
声明。
if else if
语句会更可取,因为您不会从用户那里获取新的输入来循环,但您想根据特定条件(男性或女性)应用处理。
顺便说一句,您应该将变量命名为 gender
而不是 Gender
以尊重 Java 命名约定:
if (gender == 0){
...
}
else if (gender == 1){
...
}
如果你想多次重复所有的处理,你可以使用一个循环遍历:
boolean again = false;
do{
if (gender == 0){
...
}
else if (gender == 1){
...
}
...
again = keyboardInput.nextBoolean();
} while (again);
为了退出循环,您需要在循环执行所需次数后条件变为假。
同样在上面的代码中,您似乎是在要执行的语句之间做出选择,而不是 运行 传统的循环。考虑使用 "if" 语句
while (Gender == 0){
//Do this
}
while (Gender == 1){
//Do this instead
}
如果您希望循环在打印输出语句 "X" 次后退出,您宁愿将条件基于计数变量。
所以做出选择是基于性别变量,打印语句是基于计数变量。
//Assume print is to be done 2 times
int count = 1;
if(Gender == 0){
//Female selection
while( count < 3 ){
// Execute female code
count++;
}
else if(Gender == 1){
//Male selection
while( count < 3 ){
// Execute male code
count++;
}