0
public class calculator {
static char operator;
static String operatorWord;
static String nameOfFirstNumber;
static String nameOfSecondNumber;
static double firstNumber;
static double secondNumber;
static int response2;

public static void setResponse2(boolean b){
    if(b = true){
        response2 = JOptionPane.YES_OPTION;
    }
    else if(b = false){
        response2 = JOptionPane.NO_OPTION;
    }
    else;
}

public static void setOperator(char c){
    operator = c;
}

public static void setOperatorWord(String s) throws NotAnOperatorException{
    operatorWord = s;
}


public static String getOperatorWord(char c, int i) throws NotAnOperatorException{
    if(c == '*'){
        return "Multiplication";
    }
    else if(c == '/'){
        return "Division";
    }
    else if(c =='+'){
        return "Addition";
    }
    else if(c == '-'){
        return "Subtraction";
    }
    else if(c == '%'){
        return "Remainder";
    }
    if(i < 2){
        throw new NotAnOperatorException();
    }
    return null;
}

public static void defineNameOfNumbers(char c){
    if(c == '*'){
        nameOfFirstNumber = "multiplicand";
        nameOfSecondNumber = "multiplier";
    }
    else if(c == '/'||c == '%'){
        nameOfFirstNumber = "dividend";
        nameOfSecondNumber = "divisor";
    }
    else if(c == '+'){
        nameOfFirstNumber = "addend";
        nameOfSecondNumber = "addend";
    }
    else if(c == '-'){
        nameOfFirstNumber = "subtrahend";
        nameOfSecondNumber = "minuend";
    }
}

public static double getRemainder(){
    if(operator == '/'||operator == '%'){
        return firstNumber%secondNumber;
    }
    else{
        return (Double) null;
    }
}

public static double getAnswer(){
    if(operator == '*'){
        return firstNumber*secondNumber;
    }
    else if(operator == '/'){
        return firstNumber/secondNumber;
    }
    else if(operator == '%'){
        return firstNumber%secondNumber;
    }
    else if(operator == '+'){
        return firstNumber+secondNumber;
    }
    else if(operator == '-'){
        return firstNumber-secondNumber;
    }
    else{
        return (Double) null;
    }
}

public static String getRemainderString(){
    //get the remainder & string...//
    if(operator == '/'){
        int answer = (int) (firstNumber/secondNumber);
        int remainder = (int) (firstNumber%secondNumber);
        return "\n"+answer+" remainder "+remainder;
    }
    else{
        return "";
    }
}



/**
 * @param args
 * @throws NotAnOperatorException 
 * @throws HeadlessException 
 */
public static void main(String[] args) throws HeadlessException, NotAnOperatorException {
    //end of catch statements//
    do{

        //get the operator etc. I know it's a bit overdone.//
        try{
            char operatorThing = JOptionPane.showInputDialog("Operator:").charAt(0);
            setOperator(operatorThing);
            setOperatorWord(getOperatorWord(operator, 1));
            defineNameOfNumbers(operatorThing);
            }
        //catch a NotAnOperatorException (defined with NotAnOperatorException.class)//
            catch(NotAnOperatorException exc){
                Writer writer = new StringWriter();
                PrintWriter printWriter = new PrintWriter(writer);
                exc.printStackTrace(printWriter);
                String excSentence = writer.toString();
                JOptionPane.showMessageDialog(null,"Something went wrong... Here's the rundown on what happened:\n"+excSentence+"\nWe've figured out what happened, though.\nThat's not an operator...\nRestart to rerun it, it will exit for safety.");
                System.exit(0);
            }
        //catch a null pointer//
            catch(NullPointerException exc){
                JOptionPane.showMessageDialog(null,"You clicked Cancel. Press OK to quit.");
                System.exit(0);
        }
        //catch an other exception//
            catch(Exception exc){
                Writer writer = new StringWriter();
                PrintWriter printWriter = new PrintWriter(writer);
                exc.printStackTrace(printWriter);
                String excSentence = writer.toString();
                JOptionPane.showMessageDialog(null,"Something went wrong... Here's the rundown on what happened:\n"+excSentence+"\nI have no idea what happened.\nRestart to rerun it, it will exit for safety.");
                System.exit(0);
            }
        //get the response with "int response"//
        int response;
            response = JOptionPane.showConfirmDialog(null, "Would you like to start "+getOperatorWord(operator, 2)+" Calc?");
    if(response == JOptionPane.YES_OPTION){
        String numberOne = JOptionPane.showInputDialog("X"+operator+"Y=Z\nEnter the "+nameOfFirstNumber+" (the first number.)");
        firstNumber = Double.parseDouble(numberOne);
        String numberTwo = JOptionPane.showInputDialog(firstNumber+operator+"Y=Z\nEnter the "+nameOfSecondNumber+" (the second number.)");
        secondNumber = Double.parseDouble(numberTwo);

        if(operator == '/'||operator == '%'){
            int response3 =
            JOptionPane.showConfirmDialog(null, firstNumber+operator+secondNumber+"="+getAnswer()+getRemainderString()+"\n Would you like to start "+getOperatorWord(operator, 2)+" Calc again?");
            if(response3 == JOptionPane.YES_OPTION){
                setResponse2(true);
            }
            else if(response3 == JOptionPane.NO_OPTION||response3 == JOptionPane.CANCEL_OPTION){
                setResponse2(false);
                System.exit(0);
            }
        }
        else{
            int response3 =
            JOptionPane.showConfirmDialog(null, firstNumber+operator+secondNumber+"="+getAnswer()+"\n Would you like to start "+getOperatorWord(operator, 2)+" Calc again?");
            if(response3 == JOptionPane.YES_OPTION){
                setResponse2(true);
            }
            else if(response3 == JOptionPane.NO_OPTION||response3 == JOptionPane.CANCEL_OPTION){
                setResponse2(false);
                System.exit(0);
            }
        }
    }


    }
    while(response2 == JOptionPane.YES_OPTION);


}

}

好的,我知道这是一大堆代码。抱歉,但是...当对话框显示时"X"+operator+"Y=Z\nEnter the "+nameOfFirstNumber+" (the first number.)",如果我输入 1(在下一个对话框中),它会说"48.0Y=Z"(如果它处于除法模式。)它甚至不显示操作员我也没有输入 48,我输入了 1。在其他模式下,它显示从 40 到 50 的某个数字。这是怎么回事?这很奇怪……

谢谢!

[编辑]好的,它与 ASCII 有关。但是我该如何解决呢?
[编辑]为什么它不显示操作员?

4

2 回答 2

3

快速提示:48是字符的ASCII代码049对于1,50对于2

此错误的来源是如下片段:

JOptionPane.showInputDialog(firstNumber+operator+"Y=Z\nEnter the "....

从而firstNumber将(双精度值)“添加”到字符值中。
(请注意在整个源文件中多次找到此模式)。

要解决此问题,您需要...
- 使运算符变量成为字符串变量
- 使用 String.valueOf(whatever_numeric_type_variable_at_hand)
-使用更好的习惯用法来构建您的字符串,例如:
      "%f %x Y=Z...".format(firstNumber, operator)

最好的方法可能是后者,string.format()因为它可以更好地控制字符串的格式化方式,并且可以更清楚地了解什么是可变文本,什么是固定文本。这还引入了根据文化区域支持不同格式的能力。

于 2012-10-27T05:05:28.830 回答
1

使用方法包装你的数字,String.valueOf()例如

String numberTwo = 
 JOptionPane.showInputDialog(String.valueOf(firstNumber)+operator+"Y=Z\nEnter the "
                       +nameOfSecondNumber+" (the second number.)");
于 2012-10-27T05:19:00.530 回答