[英]why doesn't parsed Double behave correctly?
I have a simple calculator here with a switch statement that should allow the user to enter a string like so: *5 我这里有一个带有switch语句的简单计算器,该语句应允许用户输入如下字符串:* 5
the operator can be - + / or * The code performs correctly during addition, however, when I subtract a value it adds it, and dividing or multiplying will result in the exceptions listed below. 该运算符可以是-+ /或*该代码在加法期间可以正确执行,但是,当我减去一个值时,它会将其相加,并且除法或乘法运算将导致下面列出的异常。
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)
I believe I am not parsing the double correctly for some reason but am unsure how to do so...I was thinking stringtokenizer but how can I use the tokenizer without a delimiter? 我相信由于某种原因我不能正确解析双精度型,但是不确定如何做...我在想stringtokenizer,但是如何在没有定界符的情况下使用tokenizer? Here is my calculator class and below that is my main class
这是我的计算器课,下面是我的主课
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);
}
}
This line is wrong: 这行是错误的:
double theNumber = Double.parseDouble(operatorString);
because it parses the operator as part of the number. 因为它将运算符解析为数字的一部分。 Instead, use this:
而是使用以下命令:
double theNumber = Double.parseDouble(operatorString.substring(1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.