Switch 语句错误? [英] Switch Statement Buggy?

查看:39
本文介绍了Switch 语句错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
无间断切换

我正在制作一个小型计算器应用程序,当点击计算按钮时,会运行一个 switch 语句,但生成的数字不是它应该的数字,我已经测试了我的输入,它们在这里工作的是 switch 语句:

I am making a small calculator app, and when the calculate button is clicked, a switch statement runs, but the number produced is not what It should be, I have tested my inputs and they do work here is the switch statement:

switch (currentOp) 
{
    case "+" : 
        answer = Float.toString(inp1 + inp2);
    case "-" : 
        answer = Float.toString(inp1 - inp2);
    case "X" : 
        answer = Float.toString(inp1 * inp2);
    case "/" : 
        answer = Float.toString(inp1 / inp2);
}

推荐答案

参见 案例失败.如果你没有在每种情况之间中断,下一个也会被执行,直到达到 break.

See case fallthrough. If you don't break between each case, the next will be executed as well until a break is reached.

break 语句是必要的,因为没有它们,在switch 块失败:匹配 case 之后的所有语句标签是按顺序执行的,不管表达式是什么后续 case 标签,直到遇到 break 语句.

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

这篇关于Switch 语句错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆