[英]I am learning to use android studio to make an app, so I am making a simple calculator, but I have run into a problem getting the correct answer
I have tried several things to get it to work, but the answers it is returning are very wrong.我已经尝试了几件事来让它工作,但它返回的答案是非常错误的。 For example, it returns 1+2 = 99.0 and 1*2 = 2450.0, etc.. All of the buttons work and the main method is fine I think.
例如,它返回 1+2 = 99.0 和 1*2 = 2450.0 等。所有按钮都可以工作,我认为 main 方法很好。 Below is the way that I am converting the CharSequence in the TextView into a String when the equals button is clicked.
下面是我在单击 equals 按钮时将 TextView 中的 CharSequence 转换为 String 的方式。
Button btnEquals = (Button)findViewById(R.id.btnEquals);
btnEquals.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String problem = textDisplay.getText().toString();
double intermediate = evalProblem(problem);
String answer = "" + intermediate;
textDisplay.setText(answer);
}
});
And this is the method that I am calling in order to calculate the equation.这就是我为了计算方程而调用的方法。
public static Double evalProblem(String s) {
double output;
double mult = 0.0;
double add = 0.0;
double sub = 0.0;
double div = 0.0;
if (s.contains("*")) {
int x = s.indexOf("*");
mult = s.charAt(x-1)*s.charAt(x+1);
}
if (s.contains("/")) {
int x = s.indexOf("/");
div = s.charAt(x - 1) / s.charAt(x + 1);
}
if (s.contains("+")) {
int x = s.indexOf(("+"));
add = s.charAt(x-1) + s.charAt(x+1);
}
if (s.contains("-")) {
int x = s.indexOf("-");
sub = s.charAt(x - 1) - s.charAt(x + 1);
}
output = mult + add + sub + div;
return output;
}
.charAt()
returns a character, not an integer. .charAt()
返回一个字符,而不是一个整数。 For example, "8*8" has 3 characters - '8', '*', and '8'.例如,“8*8”有 3 个字符 - '8'、'*' 和 '8'。 When you perform a mathematical operation with a character, it uses the ASCII value of the character eg:
'A'
has an ASCII value of 65. '0'
has a value of 48.当您对字符执行数学运算时,它使用字符的 ASCII 值,例如:
'A'
的 ASCII 值为 65。 '0'
的值为 48。
Thus, '1' + '2' = 49 + 50 = 99因此,'1' + '2' = 49 + 50 = 99
You need to convert the String part before the '*' into a number, right?您需要将'*'之前的字符串部分转换为数字,对吗? Java has an easy solution for that: the
Double.parseDouble()
method: Java 有一个简单的解决方案:
Double.parseDouble()
方法:
String text = "567";
int number = Double.parseDouble(text);
To solve your particular question, you should try this:要解决您的特定问题,您应该尝试以下操作:
int x = s.indexOf('*');
mult = Double.parseDouble(s.substring(0, x)) * Double.parseDouble(s.substring(x+1));
The best part of this solution is that you can use numbers which have more than one digit, because the code takes everything before and after '*' in s
, and converts them to numbers.此解决方案的最佳部分是您可以使用多于一位的数字,因为代码将
s
'*' 前后的所有内容都转换为数字。
Edit: As u/markspace has mentioned in the comment below, you should consider using the in-built parsing library in Java, as seen here: Algebra equation parser for java编辑:正如 u/markspace 在下面的评论中提到的,您应该考虑使用 Java 中的内置解析库,如下所示: Algebra equation parser for java
Edit 2: I had recommended using Integer.parseInt()
to convert the string into a number, but as u/MartinZeitler has pointed out in the comment below, converting to a double value using Double.parseDouble()
would be better.编辑 2:我建议使用
Integer.parseInt()
将字符串转换为数字,但正如 u/MartinZeitler 在下面的评论中指出的那样,使用Double.parseDouble()
转换为双Double.parseDouble()
值会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.