2014-10-22 27 views
1

所以我一直在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; 
     } 

    } 

我將不勝感激,如果你能告訴我什麼是錯了,所以我的代碼最終會工作,所以,我不會讓類似的錯誤在未來!

在此先感謝

+0

什麼是'input'?你認爲'input.equals(「quit」)'做了什麼? – 2014-10-22 01:35:20

+0

你的退出信息是什麼? – 2014-10-22 01:36:00

+0

@SotiriosDelimanolis我認爲這意味着「如果用戶輸入退出,然後做一些事情」 – 2014-10-22 01:37:25

回答

0

實際上你的布爾退出在這種情況下是多餘的。

所有你需要做的是改變的環路到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 
0

嘗試設置布爾退出到真正在你的if語句當用戶鍵入退出,因此將退出while循環。

+0

'if(input.equals(「quit」)){ boolean quit = true; }' – 2014-10-22 01:42:42

+0

是否這樣?它似乎不工作? – 2014-10-22 01:43:10

+0

只需嘗試if(input.equals(「quit」)){quit = true; }' – GeekOnGadgets 2014-10-22 01:45:13

-1

您的問題是掃描儀無法使用.equals方法。您需要首先識別一個字符串變量,將其設置爲等於Scanner.ReadLine(),然後將其與「quit」字符串進行比較。就像這樣:

Scanner scan = new Scanner(System.in); 

String input = scan.ReadLine(); 

if(input.equals("quit")) 
{ 
    //close the program 
} 

讓我知道這是否對您有幫助。

+0

你是否將C#的控制檯與Scanner混淆? – 2014-10-22 01:51:15

+0

不幸的是,你提供的代碼不起作用.... – 2014-10-22 01:53:13

+0

不,我很確定這會工作。您無法將掃描器變量與字符串變量進行比較。 – Britton 2014-10-22 01:53:41

-1

也許是因爲緩存。您可以在系統退出之前執行刷新。

0

您正在收到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語句要求用戶輸入(「幫助」,「退出」,或者輸入「) 並進行基於這一點。

相關問題