为什么我的Java计算器在循环中暂停?
calculator()
已经运行后,我的程序暂停......除非输入一个输入来取消暂停程序。然后继续运行并打印出来。但是,我之前输入的用于取消暂停程序的输入存储在answer
中。请阅读我在代码中的评论以获得更好的理解。如果你仍然不明白,随时可以复制代码,看看我在说什么。为什么我的Java计算器在循环中暂停?
public static void main(String[] args) {
boolean repeat = true;
while (repeat){
calculator(); //-Program pauses after this has ran.
System.out.println("Do you wish to repeat(y/n)?"); // This does not appear unless I enter an input.
String answer = userInput.next(); //The input that I entered earlier to unpause the program gets stored into answer.
if (answer.equalsIgnoreCase("y")){
repeat = true;
} else { repeat = false;}}} //Program terminates here because the input that I used to unpause the program isn't "y".
全部下面的代码:
package calculatorAttempt;
import java.util.Scanner;
class CalculatorV2 {
static Scanner userInput = new Scanner(System.in);
public static void calculator(){
System.out.print(":");
if (userInput.hasNextInt()){
int num1 = userInput.nextInt();
System.out.print(":");
String inString = userInput.next();
System.out.print(":");
int num2 = userInput.nextInt();
System.out.print("=");
if (inString.equals("+")){
System.out.print(num1+num2);}
if (inString.equals("-")){
System.out.print(num1-num2);}
if (inString.equals("*")||(inString.equalsIgnoreCase("x"))){
System.out.print(num1+num2);}
if (inString.equals("/")){
float intTofloat = (float)num1/num2;
System.out.println(intTofloat);} }//If Integer
if (userInput.hasNextFloat()){
float num1 = userInput.nextFloat();
System.out.print(":");
String inString = userInput.next();
System.out.print(":");
float num2 = userInput.nextFloat();
System.out.print("=");
if (inString.equals("+")){
System.out.print(num1+num2);}
if (inString.equals("-")){
System.out.print(num1-num2);}
if (inString.equals("*")||(inString.equalsIgnoreCase("x"))){
System.out.print(num1*num2);}
if (inString.equals("/")){
System.out.print(num1/num2);} }//If Float
}//Public Void Calculator
public static void main(String[] args) {
boolean repeat = true;
while (repeat){
calculator();
System.out.println("Do you wish to repeat(y/n)?");
String answer = userInput.next();
if (answer.equalsIgnoreCase("y")){
repeat = true;
} else { repeat = false;}}
}//Main
}//Class
我初学者,请多多包涵:^)。谢谢。
这是因为这行:
if (userInput.hasNextFloat()){
的hasNext...()
方法Scanner
是阻挡方法。如果在扫描仪中没有空白以外的输入,它们将被阻止,等待输入。一旦输入了真实的东西(不是空格或换行符),他们就会检查它是否是一个浮点数,一个整数或其他值,然后返回一个真/假回复给你。
完成整数计算后,程序会调用hasNextFloat()
,因此它会阻塞,等待输入内容。如果这不是浮动,它将返回false,并且if
将不起作用。
您可以尝试一点点,看看:
- 如果你运行你的程序,并用浮动(比如,
17.2
),程序会显示您的Do you wish to repeat?
问题,它计算的结果之后开始。 - 如果您运行程序并以int开头,然后在得到结果后输入一个浮点数,它将打印要求您输入float操作符的
:
。
所以基本上,这不应该是if
。它应该是一个else if
结构。
哇哦。你真的知道何时使用if和else if语句。 – jazzboy 2015-03-13 12:52:25
您必须使用hasNext()
而不是hasNextInt()
并删除if(hasNextFloat())
部件。并使用Number
类,因为它同时接受整型和浮点:
if (userInput.hasNext()) {
Number num1 = userInput.nextByte();
System.out.print(":");
String inString = userInput.next();
System.out.print(":");
Number num2 = userInput.nextByte();
System.out.print("=");
switch (inString) {
case "+":
System.out.print(num1.doubleValue() + num2.doubleValue());
break;
case "-":
System.out.print(num1.doubleValue() - num2.doubleValue());
break;
case "*":
case "x":
case "X":
System.out.print(num1.doubleValue() * num2.doubleValue());
break;
case "/":
double intTofloat = num1.doubleValue()/num2.doubleValue();
System.out.println(intTofloat);
break;
default:
System.out.println(INVALID OPERATOR!);
break;
}
}
只需使用else if
而不是if
检查userInput.hasNextFloat()。
您的代码示例看起来像。
if (userInput.hasNextInt()){
int num1 = userInput.nextInt();
....
....
....
} else if (userInput.hasNextFloat()){
float num1 = userInput.nextFloat();
....
....
...
}
我正在调试你的代码,还没有完成它。我认为这是与userInput.hasNextFloat() – rsb2097 2015-03-13 12:39:52
也:把这些 如果(x == a){} if(x == b){}选项变成:if(x == a){} else if (x == b){} 它将使您的代码更易于调试。这可能是因为您没有正确计算使用的next或hasNext调用的行为。 如果在计算器的末尾添加打印语句,是否在暂停之前显示? – Stultuske 2015-03-13 12:41:58