java中的嵌套do-while循环
我正在用java编写一个简单的计算器,您可以用2个操作数执行4个基本运算。我需要的是,在if语句中,我希望执行一个循环,在该循环中,当ask继续时,用户将继续执行相同的操作,如果退出,则将通过选择其他操作再次请求继续。我的问题是,它没有在if语句中执行循环,而是执行一个循环,要求用户通过选择其他操作继续java中的嵌套do-while循环,java,if-statement,do-while,Java,If Statement,Do While,我正在用java编写一个简单的计算器,您可以用2个操作数执行4个基本运算。我需要的是,在if语句中,我希望执行一个循环,在该循环中,当ask继续时,用户将继续执行相同的操作,如果退出,则将通过选择其他操作再次请求继续。我的问题是,它没有在if语句中执行循环,而是执行一个循环,要求用户通过选择其他操作继续 import java.util.Scanner; public class calculator { public static void main(String [] args)
import java.util.Scanner;
public class calculator
{
public static void main(String [] args)
{
Scanner input = new Scanner (System.in);
double x, y;
double operation;
String getChar;
String chooseOperation;
do
{
System.out.println("Choose your operation:");
System.out.println("A. Addition");
System.out.println("B. Subraction");
System.out.println("C. Multiplication");
System.out.println("D. Division");
chooseOperation = input.nextLine();
if ((chooseOperation.equals("A")) | (chooseOperation.equals("a")))
{
do
{
System.out.println("You Choose Addition\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x+y;
System.out.println("The sum is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else if ((chooseOperation.equals("B")) | (chooseOperation.equals("b")))
{
do
{
System.out.println("You Choose Subtraction\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x-y;
System.out.println("The difference is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else if ((chooseOperation.equals("C")) | (chooseOperation.equals("c")))
{
do
{
System.out.println("You Choose Multiplication\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x*y;
System.out.println("The product is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else if ((chooseOperation.equals("D")) | (chooseOperation.equals("d")))
{
do
{
System.out.println("You Choose Division\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x/y;
System.out.println("The quotient is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else
{
System.out.println("Invalid Selection!\n\n\n");
}
System.out.println("Do you want to continue?\n\nY/N");
getChar=input.nextLine();
}while((getChar.equals("Y"))|(getChar.equals("y")));
}
}
每次读取整行内容,然后解析为
double
:
//Addition
do
{
System.out.println("You Choose Addition\n\n\n");
System.out.println("Input the first number:\n");
x = Double.parseDouble(input.nextLine());//read the whole line, then parse into double
System.out.println("Input the second number:\n");
y = Double.parseDouble(input.nextLine());//read the whole line, then parse into double
operation = x + y;
System.out.println("The sum is :" + operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar = input.nextLine();//when not reading the whole line, getChar is empty
}
while (getChar.equals("Y"));
否则,
getChar
似乎包含用户输入数字时创建的下一行字符(键入数字,然后按Enter)。很久以前我就编写了java。但我认为这是因为您只使用了一个
。我不确定,但我认为java需要两个
。因此,您的第一个if将是:
if (chooseOperation.equals("A") || chooseOperation.equals("a"))
{
}
请将代码缩减到您在测试程序时输入的内容有问题的部分?你是键入Y还是Y?如果你在if语句中引用循环,它是Y,因为我在给它一个包含位运算符的布尔表达式时遇到了困难(对于提问者),只要用
a,6,7运行程序,你就会看到问题所在。input.nextLine()
有问题。它被跳过…@OhemgiIstalLes您可以引用if…else语句中的do while循环。计算机假设你想继续做同样的操作,但是它实际上取消了我用C++中所用的相同的概念来代替“^”的循环,但是我得到了一个错误,所以我只在这个例子中使用“*”,使用<代码> < <代码>或<代码> < <代码>没有区别。(可能除了执行速度)。运算符|
和&
不仅仅是位运算符(应用于整数参数时),但也有计算两个操作数的非短路逻辑运算符,而不管第一个操作数的值是多少。使用double.parseDouble是否有包?它是double.parseDouble
,而不是double.parseDouble
。它是默认Java框架Java.lang.double
的一部分,谢谢您的帮助,它能工作吗告诉我java.lang.Double是如何工作的。顺便说一句,我还是个有听力障碍问题的学生,所以我不太擅长听教授的建议。不如给我看一个链接。给你。正如ortis所写,input.nextDouble();
的问题是仍然有一个“新行符号”“在随后的一行中,使用下一个input.nextLine();
你正在阅读它,而不是要求用户提供新的输入。除了Robert给你的链接,你可以查看Double.parseDouble
方法的源代码,但对于初学者来说可能有点复杂。