Java计算器不执行if语句
我对编程比较陌生,最近开始学习Java以便进入Android编程。我想我会创建一个非常简单的计算器来练习,但我的if语句似乎不起作用Java计算器不执行if语句,java,if-statement,calculator,Java,If Statement,Calculator,我对编程比较陌生,最近开始学习Java以便进入Android编程。我想我会创建一个非常简单的计算器来练习,但我的if语句似乎不起作用 import java.util.Scanner; public class Calculator { public static void main(String[] args) { //Create new scanner object Scanner numInput = new Scanner( System.i
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
//Create new scanner object
Scanner numInput = new Scanner( System.in );
//Enter first number
System.out.println("Please enter the first number: ");
int num1 = numInput.nextInt();
//Enter the second number
System.out.println("Please enter the second number: ");
int num2 = numInput.nextInt();
//Choose the operation to perform (+,-,*,/)
System.out.println("What operation would you like to do?");
System.out.println("Type \"+\" to add.");
System.out.println("Type \"-\" to subtract.");
System.out.println("Type \"*\" to multiply.");
System.out.println("Type \"/\" to divide.");
String opChoice = numInput.nextLine();
//Add
if (opChoice.equals("+")) {
int ans = num1 + num2;
System.out.println("Adding " + num2 + " to " + num1 + " equals " + ans + ".");
}
//Subtract
else if (opChoice.equals("-")) {
int ans = num1 - num2;
System.out.println("Subtracting " + num2 + " from " + num1 + " equals " + ans + ".");
}
//Multiply
else if (opChoice.equals("*")) {
int ans = num1 + num2;
System.out.println("Multiplying " + num2 + " with " + num1 + " equals " + ans + ".");
}
//Divide
else if (opChoice.equals("/")) {
int ans = num1 + num2;
System.out.println("Dividing " + num1 + " by " + num2 + " equals " + ans + ".");
}
}
}
我使用的是EclipseIDE,它运行良好,直到它要求执行哪个操作为止。它将显示选项,但不允许我输入任何内容(我一直在用5乘以2进行测试)
我搜索了类似的问题,并尝试了他们的建议,但似乎仍然不起作用。我会感谢任何帮助,我想这可能只是我犯的一些简单的错误,所以如果这似乎是一个愚蠢的问题,我道歉
编辑:谢谢你们的快速回复,伙计们!我很感激。是的,我修正了乘法和除法 下面的代码进行加法运算,但不进行乘法和除法运算。你能查一下来源吗
//Multiply
else if (opChoice.equals("*")) {
int ans = num1 + num2;
System.out.println("Multiplying " + num2 + " with " + num1 + " equals " + ans + ".");
}
//Divide
else if (opChoice.equals("/")) {
int ans = num1 + num2;
System.out.println("Dividing " + num1 + " by " + num2 + " equals " + ans + ".");
}
}
问题在于
nextInt()
不使用(不读取)新行字符(按[Enter]时输入的字符)。解决此问题的一种方法是在每次nextLine()之后调用nextLine()
:
另一种解决方法是使用nextLine()
(返回字符串
)读取数字,然后将其解析为int
:
//Enter first number
System.out.println("Please enter the first number: ");
int num1 = numInput.nextInt();
numInput.nextLine(); // Add this
//Enter the second number
System.out.println("Please enter the second number: ");
int num2 = numInput.nextInt();
numInput.nextLine(); // Add this
int num1 = Integer.parseInt(numInput.nextLine());
您不需要添加额外的nextLine()
,因为已经调用的nextLine()
正在使用新行字符
另外,正如@sotondolphin所建议的,您可能需要检查您的*
和/
操作。问题是当numInput.nextInt()时调用代码>时,您将获得输入的号码。。。但是它留下了换行符(\n
)。您对numInput.nextLine()的调用然后获取一个空字符串
将该调用替换为numInput.next()
将解决该问题,因为它的行为稍有不同:
公共字符串next()
查找并返回此扫描程序中的下一个完整令牌。完整标记的前面和后面是与分隔符模式匹配的输入
默认的分隔符模式是空白,其中包括\n
,输入操作后(以*
为例)输入流中的内容现在是\n*\n
我还没有遇到的有趣问题。你每天都能学到新东西!您的解决方案解决了问题,谢谢!5分钟后我会接受你的回答