添加一个 Java 字符串并将其转换为整数并得到其和
Adding a Java Strings and convert it into integer and get its sum
我需要你的帮助。我需要得到上述等式的总和,我一直坚持连接过程,这将使我添加等式的结果。
String eq = "15+5cdf-45+90$%#@";
StringBuffer s = new StringBuffer(eq);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 48 && s.charAt(i) <= 57 || s.charAt(i) == '+' || s.charAt(i) == '-') {
} else {
s.deleteCharAt(i);
i--;
}
}
System.out.println(s);
String a = new String (s);
//15+5-45+90
String y = "";
String z = "";
for (int x = 0; x < a.length(); ++x) {
if (a.charAt(x) == '+'){
a = a.substring(0, x);
y = a;
}
z = y.concat(y);
}
System.out.println(z);
你不需要做你正在做的大部分事情。
public static void main(String[] args) {
String eq = "15+5cdf-45+90$%#@";
long sum = eval(eq);
System.out.println("sum: " + sum);
}
private static long eval(String eq) {
int sign = +1;
long num = 0, sum = 0;
for (int i = 0; i < eq.length(); i++) {
char ch = eq.charAt(i);
if (ch >= '0' && ch <= '9') {
num = num * 10 + ch - '0';
} else if (ch == '-' || ch == '+') {
sum += sign * num;
sign = ch == '-' ? -1 : +1;
num = 0;
}
}
sum += sign * num;
return sum;
}
打印
65
如您所见,作为解析器计算忽略意外字符的表达式所需的一切。
注意:eval
不创建任何对象。
public class Equation {
public static void main(String[] args) {
String eq = "15+5cdf-45+90$%#@";
StringBuffer s = new StringBuffer (eq);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 48 && s.charAt(i) <= 57 || s.charAt(i) == '+'
|| s.charAt(i) == '-') {
} else {
s.deleteCharAt(i);
i--;
}
}
System.out.println(s);
String a = new String(s);
// 15+5-45+90
String term = "";
char previousOperator = '+';
long sum = 0;
boolean weHavePreviousTerm = false;
for (int x = 0; x < a.length(); x++) {
// if we got to a new operator
if (a.charAt(x) == '+' || a.charAt(x) == '-') {
if (weHavePreviousTerm) {
int previousTermValue = Integer.parseInt(term);
if (previousOperator == '-') {
previousTermValue *= -1;
}
System.out.println("Adding " + previousTermValue);
sum += previousTermValue;
}
previousOperator = a.charAt(x);
// reset term buffer
term = "";
}
else {
term += a.charAt(x);
weHavePreviousTerm = true;
}
}
// do the last operation
// if the last term didn't get reset, means we still have an operation left to do
if (term.length() > 0) {
int previousTermValue = Integer.parseInt(term);
if (previousOperator == '-') {
previousTermValue *= -1;
}
System.out.println("Adding " + previousTermValue);
sum += previousTermValue;
}
System.out.println(sum);
}
}
我需要你的帮助。我需要得到上述等式的总和,我一直坚持连接过程,这将使我添加等式的结果。
String eq = "15+5cdf-45+90$%#@";
StringBuffer s = new StringBuffer(eq);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 48 && s.charAt(i) <= 57 || s.charAt(i) == '+' || s.charAt(i) == '-') {
} else {
s.deleteCharAt(i);
i--;
}
}
System.out.println(s);
String a = new String (s);
//15+5-45+90
String y = "";
String z = "";
for (int x = 0; x < a.length(); ++x) {
if (a.charAt(x) == '+'){
a = a.substring(0, x);
y = a;
}
z = y.concat(y);
}
System.out.println(z);
你不需要做你正在做的大部分事情。
public static void main(String[] args) {
String eq = "15+5cdf-45+90$%#@";
long sum = eval(eq);
System.out.println("sum: " + sum);
}
private static long eval(String eq) {
int sign = +1;
long num = 0, sum = 0;
for (int i = 0; i < eq.length(); i++) {
char ch = eq.charAt(i);
if (ch >= '0' && ch <= '9') {
num = num * 10 + ch - '0';
} else if (ch == '-' || ch == '+') {
sum += sign * num;
sign = ch == '-' ? -1 : +1;
num = 0;
}
}
sum += sign * num;
return sum;
}
打印
65
如您所见,作为解析器计算忽略意外字符的表达式所需的一切。
注意:eval
不创建任何对象。
public class Equation {
public static void main(String[] args) {
String eq = "15+5cdf-45+90$%#@";
StringBuffer s = new StringBuffer (eq);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 48 && s.charAt(i) <= 57 || s.charAt(i) == '+'
|| s.charAt(i) == '-') {
} else {
s.deleteCharAt(i);
i--;
}
}
System.out.println(s);
String a = new String(s);
// 15+5-45+90
String term = "";
char previousOperator = '+';
long sum = 0;
boolean weHavePreviousTerm = false;
for (int x = 0; x < a.length(); x++) {
// if we got to a new operator
if (a.charAt(x) == '+' || a.charAt(x) == '-') {
if (weHavePreviousTerm) {
int previousTermValue = Integer.parseInt(term);
if (previousOperator == '-') {
previousTermValue *= -1;
}
System.out.println("Adding " + previousTermValue);
sum += previousTermValue;
}
previousOperator = a.charAt(x);
// reset term buffer
term = "";
}
else {
term += a.charAt(x);
weHavePreviousTerm = true;
}
}
// do the last operation
// if the last term didn't get reset, means we still have an operation left to do
if (term.length() > 0) {
int previousTermValue = Integer.parseInt(term);
if (previousOperator == '-') {
previousTermValue *= -1;
}
System.out.println("Adding " + previousTermValue);
sum += previousTermValue;
}
System.out.println(sum);
}
}