2014-10-22 4 views
1

그래서 지금 Java에서이 간단한 계산기로 작업하고 있습니다. 사용자가 스캐너에 "quit"을 입력하면 프로그램이 종료되기를 바랍니다. 나는 다음 줄에 내 메인 클래스의 끝 부분에 '만약'문으로이 기능을 구현하려고 :Java 코드를 수정하여 사용자가 "quit"을 입력 할 때 종료되도록하려면 어떻게합니까?

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; 
     } 

    } 

나는 그것을 감사하겠습니다! 사용자 유형 그래서 while 루프를 종료합니다 종료 할 때

+0

은 무엇인가'input' 호출 할 수 있습니다? 'input.equals ("quit")는 어떻게 생각합니까? –

+0

퇴장 메시지가 무엇입니까? –

+0

@SotiriosDelimanolis 나는 "사용자가 종료 한 다음 무언가를 입력하면" –

답변

0

실제로 부울이 경우 중복 종료합니다.

당신이해야 할 모든 변경 while (true){}에 루프 종료 사용자가 "quit"를 입력하면됩니다. 모습이 코드를 가지고.

nextDouble(); 문자열로 첫 번째 입력을 호출하지 않고 검사가 종료되지 않았거나. 그렇지 않다면 Double.parseInt()을 사용하여 double로 변환 할 수 있습니다. 그렇지 않으면 숫자 형식 예외가 발생하고 사용자 입력이 "quit"이면 전화 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 문에서 true로 종료합니다.

+0

'if (input.equals ("quit")) { 부울 quit = true; } ' –

+0

좋아요? 작동하지 않는 것 같습니까? –

+0

그냥 시도해보십시오. if (input.equals ("quit")) {quit = true; } ' – GeekOnGadgets

-1

문제는 스캐너가 .equals 메서드를 사용할 수 없다는 것입니다. 먼저 문자열 변수를 식별하고 Scanner.ReadLine()과 동일하게 설정 한 다음이를 "quit"문자열과 비교해야합니다. 다음과 같이 입력하십시오 :

Scanner scan = new Scanner(System.in); 

String input = scan.ReadLine(); 

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

이 정보가 도움이되는지 알려주십시오.

+0

스캐너를 스캐너와 C#으로 혼동 스럽습니까? –

+0

불행히도 당신이 제공 한 코드는 작동하지 않습니다 .... –

+0

아니, 나는 이것이 작동 할 것이라고 확신합니다. 스캐너 변수를 문자열 변수와 비교할 수는 없습니다. – Britton

-1

아마도 캐시 때문일 수 있습니다. 시스템 종료 전에 플러시를 수행 할 수 있습니다.

0

Double을 예상 할 때 스캐너에 문자열을 제공하기 때문에 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 루프로 간다. 당신이 무엇을 할 수 있는지 사용자 입력이에 따라 ("도움말", "종료", 또는 입력 ") 을 계속 진행을 요청 인쇄 문을 추가합니다.