JAVA中case的简单用法


public class Switchtest 
{
	public static void main(String[] args) 
	{
		
		java.util.Scanner s = new java.util.Scanner(System.in);
		System.out.println("Welcome to come here! ");
		
		System.out.print("please input the first number:");
		int num1 = s.nextInt();
		
		System.out.print("Please input fuhao:");
		String operator = s.next();
		
		System.out.print("Please input the second number:");
		int num2 = s.nextInt();
		
		int result = 0;
		switch(operator)
		{
			case "+":
				result = num1 + num2;
				break;
			
			case "-":
				result = num1 - num2;
				break;
			
			case "*":
				result = num1 * num2;
				break;
			
			case "/":
				result = num1/ num2;
				break;
			default:
			System.out.println("invalid");
		}
		System.out.println(num1 + "+" + num2 + "=" + result);
	}	
}

你可能感兴趣的:(JAVA学习笔记)