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 コード0です。 491502など

このバグの原因は、次のようなスニペットです。

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

これfirstNumberにより、(double 値) が文字値に「追加」されます。
(このパターンは、ソース ファイル全体で数回見つかっていることに注意してください)。

これを修正するには、次のいずれかを行う必要があります...
- 演算子変数を文字列変数にする
- 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 に答える