java 中的简单计算器
Simple calculator in java
我正在尝试在 java 中创建简单的计算器,用户输入第一个数字和第二个数字,然后使用 while 循环计算用户喜欢的
但是当我写例如 a 而不是求和两个数字时它打破了循环!
为什么?
package com.myproject.CalcEngine2;
import java.util.Scanner; // import the Scanner class
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter fisr number");
double val1 = myObj.nextDouble();
System.out.println("Enter fisr number");
double val2 = myObj.nextDouble();
double result = 0.0d;
String addition = "a";
System.out.println("Enter what you want to do");
String opCode = myObj.next();
while(opCode.equals("E")) {
if (opCode.equals(addition))
result = val1 + val2;
else if (opCode.equals("s"))
result = val1 - val2;
else if (opCode.equals("d"))
result = val1 / val2;
else if (opCode.equals("m"))
result = val1 * val2;
System.out.println(result);
System.out.println("Enter what you want to do"+"E for exit , a for addition , s for subtraction , d for Division , m for multiplication ");
opCode = myObj.next();
}
}
}
程序显示我写的每个字符都是假的,但我不明白为什么!?
可能是因为 while 循环除了 "E" 外都会终止?
while(opCode.equals("E")) {
仅在 "E" 时运行,但我认为你的意思是
while(!opCode.equals("E")) {
您输入的条件,
while(opCode.equals("E")){
}
不适合这种情况。它不会执行 while 循环内的块,直到您输入 E
。
所以,那么 none 你的 if else 条件满足 .
添加,
while(!opCode.equals("E")){
}
因为代码会对你有所帮助。
谢谢!
请检查while循环中的条件,如果输入的输入不是'E'.
则终止
要使其正常工作,请进行如下更改:
while(!opCode.equals("E")) {
// Operation code
}
顺便说一句,为了更清楚的操作,您可以打印如下:
System.out.println("Enter what you want to do, 'a': for addition, 's': for subtraction, 'd': for division, 'm': for multiplication ");
谢谢大家
这里是最终代码,说明将来谁将在删除区分大小写的情况下进行搜索!
package com.myprogram.CalcEngine;
import java.util.Scanner; // import the Scanner class
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter fisr number");
double val1 = myObj.nextDouble();
System.out.println("Enter fisr number");
double val2 = myObj.nextDouble();
double result = 0.0d;
String addition = "a";
System.out.println("Enter what you want to do");
String opCode = myObj.next().toLowerCase();
while(!opCode.equals("e")) {
if (opCode.equals(addition)){
result = val1 + val2;
System.out.println(result);}
else if (opCode.equals("s")){
result = val1 - val2;
System.out.println(result);}
else if (opCode.equals("d")){
result = val1 / val2;
System.out.println(result);}
else if (opCode.equals("m")){
result = val1 * val2;
System.out.println(result);}
else
System.out.println("Error please enter true character: ");
System.out.println("Enter what you want to do"+"E for exit , a for addition , s for subtraction , d for Division , m for multiplication ");
opCode = myObj.next();
}
System.out.println("Good bye! See you :) ");
}
}
有变化
String opCode = myObj.next();
到
String opCode = myObj.next().toLowerCase();
在第 15 行和
变化
opCode = myObj.next();
到
opCode = myObj.next().toLowerCase();
第 32 行
所有输入均为小写。
祝一切顺利。
我正在尝试在 java 中创建简单的计算器,用户输入第一个数字和第二个数字,然后使用 while 循环计算用户喜欢的 但是当我写例如 a 而不是求和两个数字时它打破了循环! 为什么?
package com.myproject.CalcEngine2;
import java.util.Scanner; // import the Scanner class
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter fisr number");
double val1 = myObj.nextDouble();
System.out.println("Enter fisr number");
double val2 = myObj.nextDouble();
double result = 0.0d;
String addition = "a";
System.out.println("Enter what you want to do");
String opCode = myObj.next();
while(opCode.equals("E")) {
if (opCode.equals(addition))
result = val1 + val2;
else if (opCode.equals("s"))
result = val1 - val2;
else if (opCode.equals("d"))
result = val1 / val2;
else if (opCode.equals("m"))
result = val1 * val2;
System.out.println(result);
System.out.println("Enter what you want to do"+"E for exit , a for addition , s for subtraction , d for Division , m for multiplication ");
opCode = myObj.next();
}
}
}
程序显示我写的每个字符都是假的,但我不明白为什么!?
可能是因为 while 循环除了 "E" 外都会终止?
while(opCode.equals("E")) {
仅在 "E" 时运行,但我认为你的意思是
while(!opCode.equals("E")) {
您输入的条件,
while(opCode.equals("E")){
}
不适合这种情况。它不会执行 while 循环内的块,直到您输入 E
。
所以,那么 none 你的 if else 条件满足 .
添加,
while(!opCode.equals("E")){
}
因为代码会对你有所帮助。 谢谢!
请检查while循环中的条件,如果输入的输入不是'E'.
则终止要使其正常工作,请进行如下更改:
while(!opCode.equals("E")) {
// Operation code
}
顺便说一句,为了更清楚的操作,您可以打印如下:
System.out.println("Enter what you want to do, 'a': for addition, 's': for subtraction, 'd': for division, 'm': for multiplication ");
谢谢大家 这里是最终代码,说明将来谁将在删除区分大小写的情况下进行搜索!
package com.myprogram.CalcEngine;
import java.util.Scanner; // import the Scanner class
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter fisr number");
double val1 = myObj.nextDouble();
System.out.println("Enter fisr number");
double val2 = myObj.nextDouble();
double result = 0.0d;
String addition = "a";
System.out.println("Enter what you want to do");
String opCode = myObj.next().toLowerCase();
while(!opCode.equals("e")) {
if (opCode.equals(addition)){
result = val1 + val2;
System.out.println(result);}
else if (opCode.equals("s")){
result = val1 - val2;
System.out.println(result);}
else if (opCode.equals("d")){
result = val1 / val2;
System.out.println(result);}
else if (opCode.equals("m")){
result = val1 * val2;
System.out.println(result);}
else
System.out.println("Error please enter true character: ");
System.out.println("Enter what you want to do"+"E for exit , a for addition , s for subtraction , d for Division , m for multiplication ");
opCode = myObj.next();
}
System.out.println("Good bye! See you :) ");
}
}
有变化
String opCode = myObj.next();
到
String opCode = myObj.next().toLowerCase();
在第 15 行和 变化
opCode = myObj.next();
到
opCode = myObj.next().toLowerCase();
第 32 行
所有输入均为小写。 祝一切顺利。