我做一个计算器,我遇到下一个问题。当我写“ -2”时,单击减号按钮并写“ 2”,则我必须具有“ -4”,但我的应用程序崩溃。 那是我的应用程序: img
主要(大)数字是TextView。 MainActivity.java中的TextView被编写为变量“ display”。当用户单击按钮等于(=)时,将调用下一个方法:
// make result of two numbers
public void onEqual (View v) {
display.setText(makeResult()); // set TextView result of computation
}
然后是方法'makeResult',该方法将在'display'中搜索两个数字并对其进行运算(+,-,/或*)。除此之外,我的代码中还有一个变量“ operator”。该变量保存了用户设置的当前运算符,我们以后可以使用它来计算两个数字..因此,这是该方法的代码
// make result of two numbers
private String makeResult () {
double one; // one number
double two; // two number
double result; // result of two numbers
String dis = display.getText().toString(); // take text from TextView for more comfortable(manipulation)
one = Double.parseDouble(dis.substring(0, dis.indexOf(operator) - 1)); // take the first number from the start of the string and until an index of 'operator' (we don't take 'operator')
two = Double.parseDouble(dis.substring(dis.indexOf(operator) + 2, dis.length())); // take the second number after 'operator' until the end of the string
switch (operator) {
case "+":
result = one + two;
break;
case "-":
result = one - two;
break;
case "/":
result = one / two;
break;
case "x":
result = one * two;
break;
default:
result = 0;
}
operator = "";
return new DecimalFormat("#.##########").format(result); // do format of result ( 5.0 -> 5 )
}
此外,每个“操作员”在“操作员”(+)之前和之后都有两个空间,以确保更舒适等。
那么,为什么在我设置下一个计算时(-2-2)我的应用程序崩溃? 请帮助我。我不知道该怎么做。 附言:如果我对我的英语感到抱歉,因为我来自俄罗斯) P.s(2)俄罗斯原文here中本文的原文。
错误代码
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.StringIndexOutOfBoundsException: length=6; regionStart=0; regionLength=-1
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)
at com.example.danilochagov.calc_3000.MainActivity.makeResult(MainActivity.java:48)
at com.example.danilochagov.calc_3000.MainActivity.onEqual(MainActivity.java:185)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
其中185行是字符串,其中在方法“ onEqual”中调用方法“ makeResult”。 48字符串是方法“ makeResult”执行此操作的地方
one = dis.substring(0, dis.indexOf(operator) - 1);
答案 0 :(得分:0)
这是(至少在我看来)使用正则表达式更容易的情况之一。
public String makeResult(String expression) {
double result = solve(expression);
return FORMAT.format(result);
}
public static final DecimalFormat FORMAT = new DecimalFormat("#.##########");
public static final Pattern EXPRESSION = Pattern.compile(
"([+-]?\\s*\\d+\\.?\\d*)" // first number
+ "\\s*([+-xX/])\\s*" // operator
+ "([+-]?\\s*\\d+\\.?\\d*)" // second number
);
public double solve(String expression) {
Matcher m = EXPRESSION.matcher(expression);
if (m.matches()) {
double lhs = Double.parseDouble(m.group(1));
double rhs = Double.parseDouble(m.group(3));
switch (m.group(2)) {
case "x":
case "X": return lhs * rhs;
case "/": return lhs / rhs;
case "+": return lhs + rhs;
case "-": return lhs - rhs;
}
}
return 0; // or throw exception
}
在模式中,([+-]?\\s*\\d+\\.?\\d*)
表示一个数字,可以选择在其后跟随小数部分,并在其后可以选择一个符号。 \\s*
撒在允许有空格的标记位置。