java - 分配给 y 的值 x 从未在 switch 语句中使用?
问题描述
public void operationOnClick(View view){
int operandOneValue = Integer.valueOf(operandOne.getText().toString());
int operandTwoValue = Integer.valueOf(operandTwo.getText().toString());
int resultValue = 0;
Button op = (Button)view;
String operation = op.getText().toString();
switch(operation){
// now i change the keyword 'return' with 'break'
// i got the problem.
case "+" : resultValue = operandOneValue+operandTwoValue;return;
case "-" : resultValue = operandOneValue-operandTwoValue;return;
case "*" : resultValue = operandOneValue*operandTwoValue;return;
case "/" : resultValue = operandOneValue/operandTwoValue;return;
}
result.setText(String.valueOf(resultValue));
}
从未使用分配给“resultValue”的值operandOneValue+opearndTwoValue?我无法弄清楚错误?
解决方案
在您的 switch 语句中,将 every 替换return;
为break;
退出 switch 的信号,但其余代码将运行。当您return
在 void 方法中编写时,当它到达时,意味着您正在脱离方法本身,因此不会读取超过它的代码。在这种情况下,如果您的 switch case 匹配,您将永远无法使用该setText
方法。
推荐阅读
- android - 文本位置居中 React Native
- ios - Swift:扩展,通过这个函数的所有路径都会调用自己
- javascript - 谷歌地图 - 具有独特信息窗口的多个标记
- javascript - 在 asp.net 核心应用程序中使用 Paho mqtt js 库
- c++ - 如何使用 libcurl 发送soap(xml)请求
- excel - 运行时错误 9 - 下标超出范围
- php - RewriteCond 使用 PHP 和 .htaccess
- c++ - 定义一个指针随机地使程序崩溃
- c# - 如何在异常过滤器范围内获取应用程序库 url?
- mongodb - 在mongodb中进行多次搜索并替换?