1.switch多分支結構java
1 import java.util.Scanner; 2 public class Test1{ 3 public static void main(String[] args){ 4 Scanner in=new Scanner(System.in); 5 System.out.println("請輸入你的名次:"); 6 int num1=in.nextInt(); 7 switch(num1){ 8 case 1: 9 System.out.println("武林盟主"); 10 break;//跳出 11 case 2: 12 System.out.println("峨眉掌門"); 13 break; 14 case 3: 15 System.out.println("武當掌門"); 16 break; 17 default: 18 System.out.println("逐出師門"); 19 } 20 21 } 22 }
2.if嵌套結構spa
例:輸入一個整數,判斷是偶數仍是奇數code
1 import java.util.Scanner; 2 public class Test1{ 3 public static void main(String[] args){ 4 Scanner in=new Scanner(System.in); 5 System.out.println("請輸入一個整數"); 6 int num=in.nextInt(); 7 if(num==0){ 8 System.out.println("請輸入正整數"); 9 }else{ 10 if(num%2==0){ 11 System.out.println("是偶數"); 12 }else{ 13 System.out.println("是奇數"); 14 } 15 16 } 17 18 } 19 }
3.輸入一個年份判斷是瑞年仍是平年blog
1 //年能被4整除但不能被100整除或者能被400整除的爲瑞年 2 import java.util.Scanner; 3 public class Test1{ 4 public static void main(String[] args){ 5 Scanner in=new Scanner(System.in); 6 System.out.println("請輸入一個年份"); 7 int year=in.nextInt(); 8 if(year%4==0&&year%100!=0||year%400==0){ 9 System.out.println("是瑞年"); 10 }else{ 11 System.out.println("是平年"); 12 } 13 14 } 15 }