基本计算器

时间:2013-02-06 01:46:32

标签: java calculator

我的程序出了问题。程序显示第一个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));
    }


}


}

2 个答案:

答案 0 :(得分:0)

从对话框输出中读取数字,而不是从System.in:

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

代码中不需要扫描仪。

答案 1 :(得分: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;

    }

}