<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 程序:使用`switch...case`創建一個簡單的計算器 > 原文: [https://www.programiz.com/java-programming/examples/calculator-switch-case](https://www.programiz.com/java-programming/examples/calculator-switch-case) #### 在此程序中,您將學習使用 Java 中的`switch..case`創建一個簡單的計算器。 該計算器將能夠對兩個數字進行加,減,乘和除運算。 ## 示例:使用`switch`語句的簡單計算器 ```java import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter two numbers: "); // nextDouble() reads the next double from the keyboard double first = reader.nextDouble(); double second = reader.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char operator = reader.next().charAt(0); double result; switch(operator) { case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': result = first / second; break; // operator doesn't match any case constant (+, -, *, /) default: System.out.printf("Error! operator is not correct"); return; } System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result); } } ``` 運行該程序時,輸出為: ```java Enter two numbers: 1.5 4.5 Enter an operator (+, -, *, /): * 1.5 * 4.5 = 6.8 ``` 使用`Scanner`對象的`next()`方法,將用戶輸入的`*`運算符存儲在`operator`變量中。 同樣,使用`Scanner`對象的`nextDouble()`方法,將兩個操作數 1.5 和 4.5 分別存儲在變量`first`和`second`中。 由于運算符`*`匹配何時條件`'*':`,因此程序控制跳至 ```java result = first * second; ``` 該語句計算乘積并將結果存入變量`result`; `break`語句結束`switch`語句。 最后,執行`printf`語句。 **注意**:我們使用了`printf()`方法而不是`println`。 這是因為這里我們要打印格式化的字符串。 要了解更多信息,請訪問 [Java `printf()`方法](https://www.programiz.com/java-programming/printstream#printf)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看