1、輸入一個名次,第1~4名,分別稱為冠軍、亞軍、季軍、殿軍,5名及5名以上,稱為其他名次。
import java.util.Scanner;
public class switch1
{public static void main(String[] args){Scanner s=new Scanner(System.in);System.out.println("請輸入你獲得的名次(寫阿拉伯數字):");int mingci=s.nextInt();switch(mingci){case 1:System.out.println("恭喜你獲得了冠軍!");break;case 2:System.out.println("恭喜你獲得了亞軍!");break;case 3:System.out.println("恭喜你獲得了季軍!");break;case 4:System.out.println("恭喜你獲得了殿軍!");break;default:System.out.println("恭喜你獲得了其他名次!");break; }}
}
2、輸入一個名次,第一~四名,分別稱為冠軍、亞軍、季軍、殿軍,五名及五名以上,稱為其他名次。測試能否輸入兩個字,比如“十二”。
import java.util.Scanner;
public class switch2
{public static void main(String[] args){Scanner s=new Scanner(System.in);System.out.println("請輸入你獲得的名次(寫一、二、三、四、五等):");String mingci=s.next();switch(mingci){case "一":System.out.println("恭喜你獲得了冠軍!");break;case "二":System.out.println("恭喜你獲得了亞軍!");break;case "三":System.out.println("恭喜你獲得了季軍!");break;case "四":System.out.println("恭喜你獲得了殿軍!");break;case "十二":System.out.println("恭喜你獲得了第十二名!");break;default:System.out.println("恭喜你獲得了其他名次!");break; }}
}
經測試,可以輸入兩個字。