[School experiment] Define two integers and an arithmetic character variable and use the switch statement to output the result of the calculation

aims:

Define two integers in a program and define a character variable. When the character variable is'+','-','×','÷' respectively, use the switch statement to output the operation result of these two numbers.

import java.util.Scanner;
public class Test05 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int a = 5;
        int b = 10;

        System.out.println("请输入字符: ");
        char ca = sc.next().charAt(0);

        switch(ca){
    
    
            case '+':
                System.out.println(a + b); break;
            case '-':
                System.out.println(a - b); break;
            case '*':
                System.out.println(a * b); break;
            case '/':
                System.out.println(a / b); break;
        }
    }
}

Guess you like

Origin blog.csdn.net/maikotom/article/details/108876807