2016-10-09 4 views
0

왜 이것이 작동하지 않는지 모르겠다. 여기서 컴파일 오류가 발생하지 않지만 프로그램은 항상 else 문을 반환합니다. 수술을 다른 방식으로 정의하거나 다른 방식으로 호출해야합니까?스윙을 사용한 Java 계산기

import javax.swing.JOptionPane; 

public class Calculator1 { 

public static void main(String []Args) { 

    String firstNum; 
    String operation; 
    String secondNum; 

    firstNum = JOptionPane.showInputDialog("Input a number."); 
    secondNum = JOptionPane.showInputDialog("Input another number."); 

    double num1 = Double.parseDouble(firstNum); 
    double num2 = Double.parseDouble(secondNum); 

    operation = JOptionPane.showInputDialog("Input an operation sign."); 

    if (operation == "x") { 
    System.out.println(num1 * num2); 
    } 
    if (operation == "*") { 
    System.out.println(num1 * num2); 
    } 
    if (operation == "/") { 
    System.out.println(num1/num2); 
    } 
    if (operation == ":") { 
    System.out.println(num1/num2); 
    } 
    if (operation == "+") { 
    System.out.println(num1 + num2); 
    } 
    if (operation == "-") { 
    System.out.println(num1 - num2); 
    } 
    else { 
    System.out.println("Please enter an appropriate operation sign."); 
    } 

} }

+0

operation.equals ("X")이 –

+0

보기 [계산 예] (http://stackoverflow.com/a/7441804/418556). 'ScriptEngine'을 사용해 텍스트 필드의 표현을 평가합니다. –

답변

-1
당신은 "X".equals (작동)를 사용한다

;

-1

첫째, 당신은/만약 다른 구조를 사용이 필요합니다

if (operation == "x") { 
    System.out.println(num1 * num2); 
    } 
    else if (operation == "*") { 
    System.out.println(num1 * num2); 
    } 
    else if (operation == "/") { 
    System.out.println(num1/num2); 
    } 
    // Continue... 

다음 일, 자바를, 당신은 '=='연산자와 문자열의 내용을 비교할 수 없습니다. equals 메서드를 사용해야합니다.

if (operation.equals("x")) { 
    System.out.println(num1 * num2); 
    } 
    else if (operation.equals("*")) { 
    System.out.println(num1 * num2); 
    } 
    else if (operation.equals("/")) { 
    System.out.println(num1/num2); 
    } 
    // Continue... 

왜 '=='연산자가 작동하지 않습니까?

문자열은 java의 개체이며 참조로 개체를 처리합니다. 그래서 '=='를 할 때 참조 주소를 비교하고 있습니다. 내용을 비교하려면 equals 메서드를 사용해야합니다. 귀하의 경우 문장과

switch(operation) 
{ 
    case "+": 
      System.out.println(num1 + num2); 
      break; 
    case "-": 
      System.out.println(num1 - num2); 
      break; 
    case "/": 
      System.out.println(num1/num2); 
      break; 
    case "x": 
    case "*": 
       System.out.println(num1 * num2); 
       break; 
    default: 
       System.out.println("Error!"); 
} 
+0

if/else 구조없이 @Biffen. ! = '-'작업을 할 때 오류 메시지가 출력됩니다. 올바른 작업이 있습니다. – amchacon

+0

사실입니다. 내 잘못이야. – Biffen

0

문제는 다음과 같습니다

또 다른 옵션은 다음 스위치를 사용합니다. else 문은 operation이 "-"와 같지 않으면 항상 실행됩니다. 각 if 문은 별도의 코드 블록이기 때문입니다.

if(x) {} 

if(y) {} 

if(z) {} 
else {} 

여러 if 문 대신 else if를 사용하여이 작업을 수행 할 수 있습니다.

if(x) {} 
else if(y) {} 
else if(z) {} 
else {} 

이 방법을 사용할 수는 있지만 올바른 방법은 switch 문을 사용하는 것입니다.

switch(operation) { 
    case "x": result = num1 * num2 ; 
    break; 
    case "/": result = num1/num2; 
    break; 
    case "-": result = num1 - num2; 
    break, 
    default: System.out.println(errorMessage); 
}