首页 / JAVA / Java计算器不执行if语句
Java计算器不执行if语句
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java计算器不执行if语句,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1886字,纯文字阅读大概需要3分钟。
内容图文
参见英文答案 > Using scanner.nextLine() 5个
> Scanner issue when using nextLine after nextXXX 我对编程比较陌生,最近开始学习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.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 + ".");
}
}
}
我正在使用Eclipse IDE,它运行正常,直到它询问要执行的操作.它会显示选项,但不会让我输入任何内容(我一直在测试它乘以5乘以2).
我搜索了类似的问题,并尝试了他们的建议,但它似乎仍然没有用.我会感激任何帮助,我认为这可能只是我正在制作的一些简单错误,所以如果这看起来像一个愚蠢的问题,我道歉!
编辑:谢谢你的快速反应,伙计们!我很感激.是的,我修正了乘法和除法.
内容总结
以上是互联网集市为您收集整理的Java计算器不执行if语句全部内容,希望文章能够帮你解决Java计算器不执行if语句所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。