gpt4 book ai didi

Java - 程序没有跳出循环?

转载 作者:搜寻专家 更新时间:2023-11-01 01:33:10 25 4
gpt4 key购买 nike

while (choice != 7) { 

System.out.println("--- Mathematical Calculator ---");

System.out.println("");

System.out.println("Pick an operation from the list - Use nos. 1 to 7");
System.out.println("1) Multiplication");
System.out.println("2) Division");
System.out.println("3) Addition");
System.out.println("4) Subtraction");
System.out.println("5) Find the area of a regular object");
System.out.println("6) Find the volume of a regular object");
System.out.println("7) Exit");

choice = userInput.nextInt();

switch (choice) {

case 1: {
System.out.println("");
System.out.println("You have chosen multiplication");
System.out.println("Enter a number");
double num1 = userInput.nextDouble();
System.out.println("Enter another number");
double num2 = userInput.nextDouble();

double num3 = num1 * num2;

System.out.println(num1 + " * " + num2 + " = " + num3);
num1 = num3;

while (choice2 != 5 || choice2 != 6) {

System.out.println(""); System.out.println("");
System.out.println("If you would like to build on the answer obtained - pick an operation from the list - Use nos. 1 to 4");
System.out.println("Else press 5 to return to the main menu");
System.out.println("1) Multiplication");
System.out.println("2) Division");
System.out.println("3) Addition");
System.out.println("4) Subtraction");
System.out.println("5) Start new calculation");
System.out.println("6) Exit");

choice2 = userInput.nextInt();

switch (choice2) {

case 1:
{
System.out.println("Enter number");
num2 = userInput.nextDouble();
num3 = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + num3);
num1 = num3;
break;
}

case 2:
{
System.out.println("Enter number");
num2 = userInput.nextDouble();
num3 = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + num3);
num1 = num3;
reak;
}

case 3:
{
System.out.println("Enter number");
num2 = userInput.nextDouble();
num3 = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + num3);
num1 = num3;
break;
}

case 4:
{
System.out.println("Enter number");
num2 = userInput.nextDouble();
num3 = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + num3);
num1 = num3;
break;
}

case 5: choice = 0; break;

case 6: choice = 7; break;

default: System.out.println("Invalid choice");
}
choice2 = 0;
}
break;
}

我已经发布了一小段代码。我的问题是,当我在第二个查询 (choice2) 中输入 5 或 6 时,我的程序继续循环而不是跳出循环并返回主菜单/终止程序。

希望对我做错的事情提出一些反馈。

最佳答案

您的 while (choice2 != 5 || choice2 != 6) 是错误的,因为它总是会返回 true。如果 choice2 为 5,则第二个子句为真。

更不用说您总是在循环结束时将 choice2 设置为 0,因此即使您将 || 替换为 &&,循环也会永远继续。

关于Java - 程序没有跳出循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31941364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com