Java:switch语句例子
生活随笔
收集整理的這篇文章主要介紹了
Java:switch语句例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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; }} }經測試,可以輸入兩個字。
轉載于:https://www.cnblogs.com/hfchenchao/p/4836989.html
總結
以上是生活随笔為你收集整理的Java:switch语句例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国银行最值得办的信用卡盘点 不能错过的
- 下一篇: 招商银行优酷联名信用卡额度是多少?附信用