当我运行这段代码并输入一个十进制数时,我得到Exception in thread "main" java.lang.NumberFormatException
:
public class Foatadd {
public static void main(String[] args) {
String s1, s2;
add(s1, s2);
}
public static void add(String a, String b) throws NumberFormatException {
StringBuilder buf = new StringBuilder();
int i1 = a.length() - 1;
int i2 = b.length() - 1;
double carry = 0;
while (i1 >= 0 || i2 >= 0 || carry != 0)
{
float x1 = i1 < 0 ? 0 : Float.parseFloat(Character.toString(a
.charAt(i1)));
float x2 = i2 < 0 ? 0 : Float.parseFloat(Character.toString(b
.charAt(i2)));
double sum = x1 + x2 + carry;
if (sum > 9) {
carry = 1.00;
sum = (sum - 10.0);
} else {
carry = 0.00;
}
int add = (int) sum;
buf.append(add);
i1--;
i2--;
}
buf.reverse();
System.out.println(buf);``
}
}
您有一个十进制数,但是您试图一次解析一个字符。其中一个字符,例如.
不是一个有效的浮点数。
你需要做的是找到每个数字的小数点,使两个数字对齐,例如,你不能加56.78
和12.3
,但你可以这样加56.78
和12.30
。在加了足够多的零并且小数点在同一个位置之后,您可以忽略它并将其传递给答案。
顺便说一句,你应该能够单独使用int
值来做到这一点。使用float
进行基本计算可能会使这变得更加复杂。
您的字符串s1, s2没有初始化。这里需要提供一些值
public static void add(String a, String b) {
BigDecimal result = new BigDecimal(a).add(new BigDecimal(b));
System.out.println(result);
}
package com.otsi;
进口java.util.Scanner;进口java.util.Stack;
公共类evaluatestreexpression {
public static int evaluateexpression(String expression) {
// convert string into char array
Stack<Integer> intvalue = new Stack<Integer>();
Stack<Character> charvalue = new Stack<Character>();
char[] Strtokens = expression.toCharArray();
for (int i = 0; i < Strtokens.length; i++) {
if (Strtokens[i] == ' ')
continue;
if (Strtokens[i] >= '0' && Strtokens[i] <= '9')
{
StringBuilder strbuffer = new StringBuilder();
strbuffer.append(Strtokens[i]);
while (i+1 < Strtokens.length && Strtokens[i+1] >= '0' && Strtokens[i+1] <= '9')
strbuffer.append(Strtokens[++i]);
intvalue.push(Integer.parseInt(strbuffer.toString()));
}
else if (Strtokens[i] == '(')
charvalue.push(Strtokens[i]);
else if (Strtokens[i] == ')')
{
while (charvalue.peek() != '(')
intvalue.push(OprationAdd(charvalue.pop(), intvalue.pop(), intvalue.pop()));
charvalue.pop();
}
else if (Strtokens[i] == '+' || Strtokens[i] == '-' || Strtokens[i] == '*' || Strtokens[i] == '/' || Strtokens[i] == '%')
{
while (!charvalue.empty() && precedence(Strtokens[i], charvalue.peek()))
intvalue.push(OprationAdd(charvalue.pop(), intvalue.pop(), intvalue.pop()));
charvalue.push(Strtokens[i]);
}
}
while (!charvalue.empty())
intvalue.push(OprationAdd(charvalue.pop(), intvalue.pop(), intvalue.pop()));
return intvalue.pop();
}
public static boolean precedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-') && (op1 == '*' || op1 == '%'))
return false;
else
return true;
}
public static int OprationAdd(char op, int b, int a) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new UnsupportedOperationException(" we can not divide by Zero");
return a / b;
case '%':
return a % b;
}
return 0;
}
public static void main(String[] args)
{
System.out.println("Enter the value with expression");
Scanner sc = new Scanner(System.in);
String expr = sc.nextLine();
System.out.println("Output of evaluateexpression string:::" + EvaluateStrExpression.evaluateexpression(expr));
}
} '