问题及代码:
package practice_01;
import java.util.Scanner;
public class swith {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
System.out.println("enter a number");
int number =input.nextInt();
switch(number){
case 0:System.out.println("zero");break;
case 1:System.out.println("one");break;
case 2:System.out.println("two");break;
case 3:System.out.println("three");break;
default:System.out.println("oh no");break;
}
}
}
运行结果:
enter a number
1
one
知识点分析:switch(){
case 1: ;break;
case 2: ;break
}
心得及体会:必须要加break语句,不然会执行后面所有语句,还有节约空间。