gpt4 book ai didi

java - 为什么解析的 Double 行为不正确?

转载 作者:行者123 更新时间:2023-12-02 13:42:51 25 4
gpt4 key购买 nike

我这里有一个简单的计算器,带有一个 switch 语句,应该允许用户输入如下所示的字符串:*5

运算符可以是 - +/或 *代码在加法过程中执行正确,但是,当我减去一个值时,它会将其相加,并且除法或乘法将导致下面列出的异常。

Enter an operator and a number:
+5
Enter an operator and a number:
*2
Exception in thread "main" java.lang.NumberFormatException: For input string: "*2"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at Calculator.whatOperator(Calculator.java:38)
at Calculator.aResult(Calculator.java:29)
at Main.main(Main.java:30)

我相信由于某种原因我没有正确解析 double 值,但我不确定该怎么做...我在考虑 stringtokenizer 但如何在没有分隔符的情况下使用 tokenizer?这是我的计算器类,下面是我的主类

import java.util.Scanner;
public class Calculator {
private final int RESET = 0;
private double number = 0;
private double result = 0;
private char operator;
private Scanner keyboard = new Scanner(System.in);
public Calculator(double number)
{
this.number = number;

}
public void reset()
{
this.number = RESET;
}
public double aResult(Calculator other)
{

other.whatOperator();
this.result = other.result;
return result;

}

public void whatOperator()
{
String operatorString = enterNumber();
// the error occurs here....is there a better way to do this?
double theNumber = Double.parseDouble(operatorString);
char theOperator = operatorString.charAt(0);
this.operator = theOperator;
operatorString ="";
operatorString += theOperator;

// the switch should perform the operation
switch(operatorString){
case "*":
result = getNumber() * theNumber;
break;
case "/":
result = getNumber() / theNumber;
break;
case "+":
result = getNumber() + theNumber;
break;
case "-":
result = getNumber() - theNumber;
break;
case "R":
result = RESET;
break;
case "P":
System.out.println("Goodbye");
System.exit(0);

}


}
public double add(double secondNumber)
{
result = number + secondNumber;
return result;

}
public double divide(double secondNumber)
{
result = number / secondNumber;
return result;
}
public double multiply(double secondNumber)
{
result = number * secondNumber;
return result;
}
public void subtract(double secondNumber)
{
result = number - secondNumber;
}


public double getNumber()
{
return number;
}
public void setNumber(double number)
{
this.number = number;
}
public String enterNumber()
{

System.out.println("Enter an operator and a number:");
String toString = keyboard.nextLine();
return toString;
}

}
public class Main {

public static void main (String[] args) {
Calculator a = new Calculator(0);
a.setNumber(a.aResult(a));
a.setNumber(a.aResult(a));
String theString = String.valueOf(a.getNumber());
System.out.println(theString);
}
}

最佳答案

这一行是错误的:

double theNumber = Double.parseDouble(operatorString);

因为它将运算符解析为数字的一部分。相反,使用这个:

double theNumber = Double.parseDouble(operatorString.substring(1));

关于java - 为什么解析的 Double 行为不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42641387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com