如何修复我的Java代码,以便在用户输入“quit”时结束?
所以我一直在Java中使用这个简单的计算器一段时间,并且我希望程序在用户在扫描器中输入“quit”时结束。我试图实现此功能为我的附近在以下行主类的末尾的“如果”语句:如何修复我的Java代码,以便在用户输入“quit”时结束?
if (input.equals("quit")) {
System.out.println("Thanks for using my program.");
System.exit(0);
}
我的IDE不承认任何错误,但是当我编译和运行我的程序,尝试通过在扫描仪中键入“退出”退出程序,程序结束(带有错误)而不显示退出消息。
我的代码如下:
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;
}
}
我将不胜感激,如果你能告诉我什么是错了,所以我的代码最终会工作,所以,我不会让类似的错误在未来!
在此先感谢
实际上你的布尔退出在这种情况下是多余的。
所有你需要做的是改变的环路到while (true){}
,如果用户输入“quit
”退出。看看这段代码。
不叫nextDouble();
采取先输入一个字符串,并检查其退出与否。如果不是,那么你可以使用Double.parseInt()
来隐藏它,否则会抛出一个数字格式异常,如果用户输入是“quit”,那么你可以使用c一个呼叫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
尝试设置布尔退出到真正在你的if语句当用户键入退出,因此将退出while循环。
'if(input.equals(“quit”)){ boolean quit = true; }' – 2014-10-22 01:42:42
是否这样?它似乎不工作? – 2014-10-22 01:43:10
只需尝试if(input.equals(“quit”)){quit = true; }' – GeekOnGadgets 2014-10-22 01:45:13
您的问题是扫描仪无法使用.equals方法。您需要首先识别一个字符串变量,将其设置为等于Scanner.ReadLine(),然后将其与“quit”字符串进行比较。就像这样:
Scanner scan = new Scanner(System.in);
String input = scan.ReadLine();
if(input.equals("quit"))
{
//close the program
}
让我知道这是否对您有帮助。
你是否将C#的控制台与Scanner混淆? – 2014-10-22 01:51:15
不幸的是,你提供的代码不起作用.... – 2014-10-22 01:53:13
不,我很确定这会工作。您无法将扫描器变量与字符串变量进行比较。 – Britton 2014-10-22 01:53:41
也许是因为缓存。您可以在系统退出之前执行刷新。
您正在收到InputMismatchException,因为您在给扫描器预期双精度型时会给它一个字符串。
应使用scanner.nextLine()
方法读入字符串变量。
看到的,一旦这显示:
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.
你的代码放入while循环,它不支持字符串输入。 你可以做的是增加一个print语句要求用户输入(“帮助”,“退出”,或者输入“) 并进行基于这一点。
什么是'input'?你认为'input.equals(“quit”)'做了什么? – 2014-10-22 01:35:20
你的退出信息是什么? – 2014-10-22 01:36:00
@SotiriosDelimanolis我认为这意味着“如果用户输入退出,然后做一些事情” – 2014-10-22 01:37:25