gpt4 book ai didi

java - 如何修复我的 Java 代码,使其在用户键入 "quit"时结束?

转载 作者:行者123 更新时间:2023-12-01 06:23:55 25 4
gpt4 key购买 nike

所以我已经用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com