多分支以及switch選擇結構


1.多分支

例題;小明考100買車90分以上買mp4 60分以上買書60以下啥也不買。

import java.util.Scanner;
public class jiangli {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		    System.out.print("請輸入小明的成績");
        int Score=input.nextInt();//注意別和上行換錯位置
        if(Score==100){
            System.out.print("爸爸給他買輛車");
        }else if(Score>=90){
        	System.out.print("媽媽給他買個MP4");
        }else if(Score>=60){
        	System.out.print("媽媽給他買本書");
        }else{
        	System.out.print("什么也不買");
        }
	}

}

 2switch選擇結構

輸入1顯示第一名2顯示第二名......否則沒任何人獎勵

import java.util.Scanner;
public class swh {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("請輸入名次");
        int MingCi=input.nextInt();
        switch (MingCi){
        case 1:
            System.out.print("第一名");
            break;
        case 2:
            System.out.print("第二名");
            break;
        case 3:
            System.out.print("第三名");
            break;
        default:
            System.out.print("沒人會獎勵");
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM