switch语句是多分支选择语句,用于基于不同的条件来执行不同的动作。switch case语句有些什么用法?<br>Java语言<br>swtich()变量类型只能是int、short、char、byte和enum类型。当进行case判断时,JVM会自动从上到小扫描,寻找匹配的case,可能存在多种况: 若未找到,则执行默认的case。<br>当每一个case都不存在break时,JVM并不会顺序输出每一个case对应的返回值,而是继续匹配,匹配不成功则返回默认case。<br>当每一个case都不存在break时,匹配成功后,从当前case开始,依次返回后续所有case的返回值。<br>若当前匹配成功的case不存在break,则从当前case开始,依次返回后续case的返回值,直到遇到break,跳出判断。
"# includeint main() { char operator; double firstNumber,secondNumber; printf(""输入操作符 (+, -, *,/): ""); scanf(""%c"", &operator); printf(""输入两个数字: ""); scanf(""%lf %lf"",&firstNumber, &secondNumber); switch(operator) { case '+': printf(""%.1lf + %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber + secondNumber); break; case '-': printf(""%.1lf - %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber - secondNumber); break; case '*': printf(""%.1lf * %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber * secondNumber); break; case '/': printf(""%.1lf / %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber / secondNumber); break; // operator doesn't match any case constant (+, -, *, /) default: printf(""Error! operator is not correct""); } return 0; }"