- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我已经用 Java 开发这个简单的计算器有一段时间了,我希望当用户在扫描仪中键入“quit”时程序结束。我尝试在主类末尾附近以“If”语句的形式实现此功能,如下行:
if (input.equals("quit")) {
System.out.println("Thanks for using my program.");
System.exit(0);
}
我的 IDE 无法识别任何错误,但是当我编译并运行程序,并尝试通过在扫描仪中键入“quit”来退出程序时,程序结束(有错误),而不显示退出消息。
我的代码如下:
package calculator;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
System.out.println("Hello, welcome to my calculator");
System.out.println("Enter in some stuff you want to me to calculate");
Scanner scan = new Scanner(System.in);
System.out.println("If you need help please type \"help\"");
System.out.println("If at anytime you want to leave, type \"quit\"");
System.out.println("If you want to continue, hit enter.");
String s1 = scan.nextLine();
if (s1.equals("help")){
System.out.println(" ");
System.out.println("Double operand commands:");
System.out.println("Addition: '+' (Ex: 'a + b' )");
System.out.println("Subtraction: '-' (Ex: 'a - b' )");
System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
System.out.println("Division: '/' (Ex: 'a / b' )");
System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
System.out.println(" ");
}
Scanner input = new Scanner(System.in);
Maths maths = new Maths();
double answer = 0;
double numA, numB;
char operator;
boolean quit = false;
while (quit == false) {
System.out.print("Please enter your equation: ");
numA = input.nextDouble();
operator = input.next().charAt(0);
numB = input.nextDouble();
if (operator == '+') {
answer = maths.add(numA, numB);
}
if (operator == '-') {
answer = maths.subtract(numA, numB);
}
if (operator == '*') {
answer = maths.multiply(numA, numB);
}
if (operator == '/') {
answer = maths.divide(numA, numB);
}
if (operator == '^') {
answer = maths.power(numA, numB);
}
System.out.println(answer);
if (input.equals("quit")) {
System.out.println("Thanks for using my program.");
System.exit(0);
}
}
input.close();
}
}
class Maths {
double add(double a, double b) {
double answer = a+b;
return answer;
}
double subtract(double a, double b) {
double answer = a-b;
return answer;
}
double multiply(double a, double b) {
double answer = a*b;
return answer;
}
double divide(double a, double b) {
double answer = a/b;
return answer;
}
double power(double a, double b){
double answer =a;
for (int x=2; x<=b; x++){
answer *= a;
}
return answer;
}
}
如果您能告诉我出了什么问题,以便我的代码最终能够正常工作,并且我将来不会犯类似的错误,我将不胜感激!
提前致谢
最佳答案
实际上,在这种情况下,您的 boolean 退出是多余的。
您需要做的就是将循环更改为 while (true){}
如果用户输入“quit
”则退出。看看这段代码。
不要打电话nextDouble();
将第一个输入作为字符串并检查它是否退出。如果没有,那么您可以使用 Double.parseInt()
将其转换为双倍否则将抛出数字格式异常,如果用户输入是“退出”,您可以调用 system.exit(0);
您也可以调用return;
package calculator;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
System.out.println("Hello, welcome to my calculator");
System.out.println("Enter in some stuff you want to me to calculate");
Scanner scan = new Scanner(System.in);
System.out.println("If you need help please type \"help\"");
System.out.println("If at anytime you want to leave, type \"quit\"");
System.out.println("If you want to continue, hit enter.");
String s1 = scan.nextLine();
if (s1.equals("help")){
System.out.println(" ");
System.out.println("Double operand commands:");
System.out.println("Addition: '+' (Ex: 'a + b' )");
System.out.println("Subtraction: '-' (Ex: 'a - b' )");
System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
System.out.println("Division: '/' (Ex: 'a / b' )");
System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
System.out.println(" ");
}
Scanner input = new Scanner(System.in);
Maths maths = new Maths();
double answer = 0;
double numA, numB;
char operator;
boolean quit = false;
while (true) {
System.out.print("Please enter your equation: ");
String s=input.next();
if(s.equals("quit")){
System.out.println("Thanks for using my programe");
System.exit(0);
}
numA = Double.parseDouble(s);
operator = input.next().charAt(0);
numB = input.nextDouble();
if (operator == '+') {
answer = maths.add(numA, numB);
}
if (operator == '-') {
answer = maths.subtract(numA, numB);
}
if (operator == '*') {
answer = maths.multiply(numA, numB);
}
if (operator == '/') {
answer = maths.divide(numA, numB);
}
if (operator == '^') {
answer = maths.power(numA, numB);
}
System.out.println(answer);
}
}
}
class Maths {
double add(double a, double b) {
double answer = a+b;
return answer;
}
double subtract(double a, double b) {
double answer = a-b;
return answer;
}
double multiply(double a, double b) {
double answer = a*b;
return answer;
}
double divide(double a, double b) {
double answer = a/b;
return answer;
}
double power(double a, double b){
double answer =a;
for (int x=2; x<=b; x++){
answer *= a;
}
return answer;
}
}
示例输出>>
Hello, welcome to my calculator
Enter in some stuff you want to me to calculate
If you need help please type "help"
If at anytime you want to leave, type "quit"
If you want to continue, hit enter.
Please enter your equation: 2
+
3
5.0
Please enter your equation: quit
Thanks for using my programe
关于java - 如何修复我的 Java 代码,使其在用户键入 "quit"时结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499063/
可以说我只是使用 pygame 有一个正常的游戏循环。 run = True while run: for event in pygame.event.get(): if ev
如何让控制台应用程序运行直到用户输入“Q”、“q”、“Quit”或“quit”来终止它? 这是我当前的代码: public class Class1 { [STAThread] static
我正在创建一个基于 promise 的 API。如果我一次发出一个请求,一切都会正常,但是,如果 2 个或更多请求在一秒内到达服务器,我会收到以下错误 Error: Cannot enqueue Qu
考虑: (gdb) q A debugging session is active. Inferior 1 [process 9018] will be killed. Quit an
我有以下设置: int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Create the DBM
我的停靠菜单总是自动添加“退出”和其他 2 个菜单项,我如何阻止/修改它们? 更新: 确实没有办法删除/阻止/重定向“退出”菜单项。最后像打击一样使用了彼得的推荐希望对其他人有帮助 -(NSAppli
这是我的代码:1. 用户输入两个名称,中间有一个空格。这意味着需要读取两个字符串。 IE。输入: 约翰·多伊。 然后在字符数组中检查字符串。 (工作正常)。 while 循环一直持续到用户输入“sto
刚刚弄乱了 pygame 并遇到了这个错误。 代码: import sys import pygame pygame.init() size = width, height = 600, 400 sc
看完Eric Lippert’s answer我的印象是 await和 call/cc几乎是同一枚硬币的两面,最多只是句法上的差异。然而,在尝试实际实现时 call/cc在 C# 5 中,我遇到了一个
Memcached quit 命令用户关闭一个客户端连接 语法 quit 范例 连接到127.0.0.1 上 11211 的 Memcached 服务, 然后退出 $ telnet 12
这只是一种方便,但我认为很有用。请注意,IPython 和 Matlab 一样允许纯退出。因此,在 Julia 中允许混叠是合理的。 感谢您提供有关如何执行此操作的任何想法。 最佳答案 退出 Juli
我正在运行Django服务,该服务将启动chromedriver用于 Selenium ,并爬取网站以获取数据。 另一个Java服务通过HTTP调用Django服务。 这是代码: views.py p
Iam在我的游戏中一起使用Adcolony和Vungle时,一切正常,但是当用Application.Quit()游戏退出游戏时,关闭了游戏,但弹出了文本“不幸的应用程序崩溃”的弹出窗口。 在这里我附
我最近做了一个小游戏,然后用pyinstaller把它变成了一个应用程序。运行时,有两种关闭方式。它是全屏的,因此不包括单击红色 x 按钮,但基本上我有这个 if keys[pygame.K_ESCA
我正在练习使用递归,但有些东西我不太明白。例如,我写了这个简单的倒计时函数,它应该等到一秒过去,然后倒计时到下一秒。 我首先是这样写的: function countdown(sec) { con
我正在使用 Appium 安装应用程序。安装后,应用程序应该在后台运行。另一项测试应使用 Chrome 在应用程序在后台运行时检查互联网连接。问题是 driver.quit() 甚至启动 Androi
我有一个正在运行测试的框架;任何错误都会导致 WebDriver 实例调用类似 handleException 方法的方法,我在其中调用 driver.quit() 方法。我觉得 driver.qui
我知道所有基本的处理程序,即 on run、on open 和 onreopen。但是这个处理程序,on quit,让我很困惑。我的问题是,它的用途是什么以及它是如何触发的? 最佳答案 on quit
我有两个 for 循环: for event in pygame.event.get(): if event.type == QUIT:
我目前正在编写一个小的 GUI 程序,它可以完成一些工作并在之后退出。工作完成后,GUI 线程会更新用户的信息。 这是我目前使用的模式,我认为它不是最优雅的模式: static void MainFo
我是一名优秀的程序员,十分优秀!