我在java中编写一个简单的计算器,只需2个操作数即可执行4个基本操作。我需要的是,在if语句中,我想执行一个循环,用户在要求继续时将继续执行相同的操作,如果它退出,将再次询问是否继续选择其他操作。我的问题是它没有在if语句中执行循环,而是执行循环,用户通过选择其他操作来要求继续。
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")));
}
}
答案 0 :(得分:1)
每次阅读整行,然后解析为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键)。
答案 1 :(得分:0)
很久以前我编写了java。但我认为这是因为你只使用了一个|
。我不确定,但我认为java需要两个|
。所以你的第一个将是:
if (chooseOperation.equals("A") || chooseOperation.equals("a"))
{
}