所以我刚刚开始使用其他几种语言的一点点经验。我试图制作这个基本的计算器,遇到了很多问题,但设法解决了大部分问题。我似乎无法理解的最后一件事是每次我的程序运行一次时随机触发的“这是一个无效的输入”。“...”指的是不相关的代码。其他一切似乎都很好。提前致谢!
import java.util.Scanner;
public class Calc {
...
System.out.println("Would you like to use the calculator?(Y/N)");
while(use){
String usage=in.nextLine().toLowerCase();
if(usage.equals("n")){use=false;}
//input
//operations
else if(usage.equals("y")){
...(calculator code)
System.out.println("Continue use? (Y/N)");
}
else {System.out.println("That is not a valid input");}
}
}
}
运行我的代码几次后,我的输出是
Would you like to use the calculator?(Y/N)
Y
Please input an operation: +,-,*,/,%, ^, or root
+
Calculator: Please input your first number.
1
Now enter your second number.
2
Calculating
3.0
Continue use? (Y/N)
That is not a valid input <-- right there is the confusing part, why is that triggered?
Y
Please input an operation: +,-,*,/,%, ^, or root
如果您需要,完整的代码在 pastebin 上。http://pastebin.com/Qee2Hxe3