2014-10-08 3 views
0

제발 도와주세요! 나는 필사적이다.JOptionPane 기호를 찾을 수 없습니다

import javax.swing.JOptionPane; 

public class Calculator { 
    public static void main (String args[]) { 


    double n1 = Double.parseDouble(JOptionPane.showInputDialog(" Enter first number: ")); 
    double n2 = Double.parseDouble(JOptionPane.showInputDialog(" Enter second number: ")); 
    String x = JOptionPane.showInputDialog("Enter operator: "); 
    double result; 

    if (x == "+"){ 
     result = (n1 + n2); 
      JOptionPane.showMessageDialog(n1, "+", n2,"=", result); 
      } 

     else if (x == "-"){ 
     result = (n1 - n2); 
      JOptionPane.showMessageDialog(n1,"-", n2,"=", result); 
      } 


     else if (x == "/"){ 
      result = (n1/n2); 
      JOptionPane.showMessageDialog(n1,"/", n2,"=", result); } 
     else if (n2 == 0){ 
       JOptionPane.showMessageDialog(null, "Cannot divide by 0"); } 


     else if (x == "*"){ 
      result = (n1 * n2); 
      JOptionPane.showMessageDialog(n1,"*", n2,"=", result); } 


     else if (x == "%"){ 
       result = (n1 % n2); 
       JOptionPane.showMessageDialog(n1,"%", n2,"=",(n1%n2)); } 
    } 
} 

내 코드는 다음 컴파일 오류가 나타납니다 :

5 오류 발견 : 파일 :/사용자/gcaruso/문서/CISS 110/모듈 3/모듈 4/Calculator.java [선 : 14] 오류 :/사용자/gcaruso/문서/CISS 110/모듈 3/모듈 4/Calculator.java : 14 : 심볼 기호를 찾을 수 없습니다. showMessageDialog (double, java.lang.String, double, java.lang.String, double) 위치 : 클래스 javax.swing.JOptionPane 파일 :/Users/gcaruso/Documents/CISS 110/모듈 3/모듈 4/Calculator.java [행 : 19] 오류 :/Users/g 예 : CISS 110/모듈 3/모듈 4/Calculator.java : 19 : 심볼을 찾을 수 없습니다. 심볼 : 메소드 showMessageDialog (double, java.lang.String, double, java.lang.String, double) 위치 : class/Users/gcaruso/Documents/CISS 110/Module 3/Module 4 (모듈/모듈 3/모듈 4/Calculator.java 파일/사용자/gcaruso/문서/CISS 110/모듈 4/Calculator.java [행 : 25] ) /Calculator.java:25 : 기호를 찾을 수 없습니다. 심볼 : 메소드 showMessageDialog (double, java.lang.String, double, java.lang.String, double) 위치 : 클래스 javax.swing.JOptionPane 파일 :/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [행 : 32] 오류 :/사용자/gcaruso/Documents/CISS 110/모듈 3/모듈 4/Calculator.java : 32 : 기호를 찾을 수 없음 sym 메소드 : showMessageDialog (double, java.lang.String, double, java.lang.String, double) 위치 : 클래스 javax.swing.JOptionPane 파일 :/사용자/gcaruso/Documents/CISS 110/모듈 3/모듈 4 /Calculator.java 오류 :/Users/gcaruso/Documents/CISS 110/모듈 3/모듈 4/Calculator.java : 37 : 심볼 심볼을 찾을 수 없습니다. 메소드 showMessageDialog (double, java.lang.String , double, java.lang.String, double) 위치 : 클래스 javax.swing.JOptionPane

JOptionPane을 사용하여 간단한 계산기를 만들려고합니다. 이 일에 7 시간 째 다가 오면서 성공을 거두지 못했습니다. 누군가 도와달라고 부탁합니다. 제발, 제발, 제발.

답변

2

그래서 모든 게 작동합니다

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class Calculator extends JPanel { 
    public static void main(String args[]) { 

     JFrame f = new JFrame(); 

     double n1 = Double.parseDouble(JOptionPane 
       .showInputDialog(" Enter first number: ")); 
     double n2 = Double.parseDouble(JOptionPane 
       .showInputDialog(" Enter second number: ")); 
     String x = JOptionPane.showInputDialog("Enter operator: "); 
     double result; 

     if (x.equals("+")) { 
      result = (n1 + n2); 
      JOptionPane.showMessageDialog(f, n1 + "+" + n2 + "=" + result); 
      System.out.println(x); 
     } 

     else if (x.equals("-")) { 
      result = (n1 - n2); 
      JOptionPane.showMessageDialog(f, n1 + "-" + n2 + "=" + result); 
      System.out.println(x); 
     } 

     else if (x.equals("/")) { 
      if (n2 == 0) { 
       JOptionPane.showMessageDialog(null, "Cannot divide by 0"); 
      }else{ 
      result = (n1/n2); 
      JOptionPane.showMessageDialog(f, n1 + "/" + n2 + "=" + result); 
      System.out.println(x); 
      } 
     } 

     else if (x.equals("*")) { 
      result = (n1 * n2); 
      JOptionPane.showMessageDialog(f, n1 + "*" + n2 + "=" + result); 
     } 

     else if (x.equals("%")) { 
      result = (n1 % n2); 
      JOptionPane.showMessageDialog(f, n1 + "%" + n2 + "=" + (n1 % n2)); 
     } 
    } 
} 

" == "는". equals "와 동일하지 않습니다. -

.equals는

==들이 자바 ~에서 같은 객체 경우

+0

충분히 감사드립니다! –

+0

참고로'JFrame f '를 제공 할 필요가 없습니다. 당신은'null'을 사용할 수 있으며 대화 상자는 정상적으로 작동합니다. 첫 번째 인수는 부모 소유자 IIRC입니다. 무언가에 부모가 없으면 그것을 줄 필요가 없습니다. – Compass

+0

오, 나는 그걸 알지 못했습니다. –

0

그래서 난 당신이 방법 showMessageDialog의 잘못했을 생각 :

JOptionPane.showMessageDialog(n1, "+", n2,"=", result); 

을보십시오이 : 나는 그것을 마무리

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class Calculator extends JPanel { 
    public static void main(String args[]) { 

     JFrame f = new JFrame(); 

     double n1 = Double.parseDouble(JOptionPane 
       .showInputDialog(" Enter first number: ")); 
     double n2 = Double.parseDouble(JOptionPane 
       .showInputDialog(" Enter second number: ")); 
     String x = JOptionPane.showInputDialog("Enter operator: "); 
     double result; 

     if (x == "+") { 
      result = (n1 + n2); 
      JOptionPane.showMessageDialog(f, n1 + "+" + n2 + "=" + result); 
     } 

     else if (x == "-") { 
      result = (n1 - n2); 
      JOptionPane.showMessageDialog(f, n1 + "-" + n2 + "=" + result); 
     } 

     else if (x == "/") { 
      result = (n1/n2); 
      JOptionPane.showMessageDialog(f, n1 + "/" + n2 + "=" + result); 
     } else if (n2 == 0) { 
      JOptionPane.showMessageDialog(null, "Cannot divide by 0"); 
     } 

     else if (x == "*") { 
      result = (n1 * n2); 
      JOptionPane.showMessageDialog(f, n1 + "*" + n2 + "=" + result); 
     } 

     else if (x == "%") { 
      result = (n1 % n2); 
      JOptionPane.showMessageDialog(f, n1 + "%" + n2 + "=" + (n1 % n2)); 
     } 
    } 
} 
+0

감사합니다 비교하여 두 개체의 값을 비교! 이제 컴파일 중이지만 여전히 출력이 표시되지 않습니다. –

+0

새로운 답변 ^^ :) –

+0

사이드 노트로 자바 7의 문자열에 스위치/케이스를 사용할 수 있습니다. – IdusOrtus

관련 문제