2013-02-06 112 views
0

我的程序有問題。程序顯示第一個JOption消息對話框,但是當你輸入一個值時,它不能顯示第二個對話框?基本計算器

import java.util.Scanner; 
import javax.swing.JOptionPane; 

public class javaCalculator 
{ 
public static void main(String[] args) 
{ 
    int num1; 
    int num2; 
    String operation; 


    Scanner input = new Scanner(System.in); 

    JOptionPane.showInputDialog(null,"please enter the first number"); 
    num1 = input.nextInt(); 

    JOptionPane.showInputDialog(null,"please enter the second number"); 
    num2 = input.nextInt(); 


    JOptionPane.showInputDialog(null,"Please enter operation"); 
    operation = input.next(); 

    if (operation.equals ("+")) 
    { 
     JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 + num2)); 
    } 
    if (operation.equals ("-")) 
    { 
     JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 - num2)); 
    } 

    if (operation.equals ("/")) 
    { 
     JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1/num2)); 
    } 
    if (operation.equals ("*")) 
    { 
     JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 * num2)); 
    } 


} 


} 
+0

然後操作可能*不*等於任何操作字符串 - 什麼是* *是真的嗎? (另外,考慮使用'System.out.println'作爲控制檯應用程序..你正在從控制檯讀取數據,所以不妨寫信給它: - /) – 2013-02-06 01:54:34

+0

而且,*這個程序是如何啓動的?用'java'或'javaw'? – 2013-02-06 02:01:16

回答

0

從對話框輸出,而不是從System.in閱讀數:

String firstNumber = JOptionPane.showInputDialog(null,"please enter the first number"); 
if (firstNumber != null) { 
    num1 = Integer.parseInt(firstNumber); 
} 

掃描儀是在你的代碼不必要的。

+0

@ user2037720它有用嗎?請接受或說點什麼:) – gincsait

+0

無需糾正OP的反饋/接受。 –

0

您可以嘗試使用示例代碼:

import javax.swing.JOptionPane; 

public class Hw4_3 { 

    private static boolean flag; 

    public static void main(String[] args) { 
     do { 
      String[] expression = getTask(); 
      double result = calculate(expression); 
      display(expression, result); 
      repeat(); 
     } while (flag); 
    } 

    public static String[] getTask() 
    { 
     String bleh = JOptionPane.showInputDialog(null, "Please enter what you would like to calculate: "); 
     String[] tokens = bleh.split(" "); 
     return tokens; 
    } 

    public static double calculate(String[] data) 
    { 
     double num1 = Double.parseDouble(data[0]); 
     double num2 = Double.parseDouble(data[2]); 
     double result = 0; 
     if(data[1].equals("+")) 
     { 
      result = add(num1, num2); 
      return result; 
     } 
     else if(data[1].equals("-")) 
     { 
      result = subtract(num1, num2); 
      return result; 
     } 
     else if(data[1].equals("*")) 
     { 
      result = multiply(num1, num2); 
      return result; 
     } 
     else 
     { 
      if(num2 == 0) 
      { 
       JOptionPane.showMessageDialog(null, "Sytax error, divide by zero"); 
      } 
      result = divide(num1, num2); 
      return result; 
     } 


    } 

    public static double add(double num1, double num2) 
    { 
     return num1 + num2; 
    } 

    public static double subtract(double num1, double num2) 
    { 
     return num1 - num2; 
    } 

    public static double multiply(double num1, double num2) 
    { 
     return num1 * num2; 
    } 

    public static double divide(double num1, double num2) 
    { 
     return num1/num2; 
    } 

    public static void display(String[] data, double result) 
    { 
     if(data[1].equals("/") && data[2].equals("0")) 
     { 

     } 
     else 
     { 
      JOptionPane.showMessageDialog(null, data[0] + " " + data[1] + " " + data[2] + " = " + result); 
     } 
    } 

    public static void repeat() 
    { 
     int bleh = JOptionPane.showConfirmDialog(null, "More?",null, JOptionPane.YES_NO_OPTION); 
     if(bleh == JOptionPane.NO_OPTION) 
     { 
      flag = false; 
      JOptionPane.showMessageDialog(null, "Have a good day."); 
     } 
     else 
      flag = true; 

    } 

} 
+1

歡迎來到Stack Overflow!通常情況下,如果答案包含解釋代碼意圖做什麼的解釋,以及爲什麼解決問題而不介紹其他問題,答案會更有幫助。 (這個也會從正確的縮進中受益。) –