1、輸入一個整數(shù),如果此數(shù)為0,則輸出”石頭”,如果此數(shù)為1,則輸出”剪刀”,如果此數(shù)為2,則輸出”布”,如果為其它,則輸出”錯誤”
public?class?IfTest1?{static?int?number;public?static?void?main(String[]?args)?{????System.out.println("請輸入一個整數(shù):");????Scanner?sc?=?new?Scanner(System.in);????number?=?sc.nextInt();????if(number==0){????????System.out.println("石頭");????}else?if(number==1){????????System.out.println("剪刀");????}?else?if(number==2){????????System.out.println("布");????}else{????????System.out.println("錯誤");????????}????}?}
2、輸入三個數(shù)比較大小,輸出最大的數(shù)
public?class?IfTest2?{public?static?void?main(String[]?args)?{System.out.println("請輸入");??Scanner?sc?=?new?Scanner(System.in);??String?x=sc.nextLine();??String?y=sc.nextLine();??String?z=sc.nextLine();??Double?a=Double.parseDouble(x);??Double?b=Double.parseDouble(y);??Double?c=Double.parseDouble(z);??if(a>b){???if(a>c){????System.out.println(a);???}???else???{????System.out.println(c);;???}??}??else??{???if(b>c){????System.out.println(b);???}???else???{????System.out.println(c);???}??????}}}
3、輸入一個整數(shù),如果輸入的數(shù)大于等于1000,那么輸出“輸入的數(shù)>=1000”,否則的話就輸出“輸入的數(shù)<1000”。
public?class?IfTest3?{static?int?number;public?static?void?main(String[]?args)?{????System.out.println("輸入一個整數(shù):");????Scanner?sc?=?new?Scanner(System.in);????number?=?sc.nextInt();????if(number>=1000){????????System.out.println("輸入的數(shù)>=1000");????}else{????????System.out.println("輸入的數(shù)<1000");???????}???}}
4、輸入三個int型的數(shù)據(jù),放入到a,b,c三個變量中去,使用條件結(jié)構(gòu)與交換邏輯將這三個變量中的值從小到大排列。
public?class?IfTest4?{static?int?a,b,c,max,middle,min;public?static?void?main(String[]?args)?{????System.out.println("輸入三個整數(shù):");????Scanner?sc?=?new?Scanner(System.in);????a?=?sc.nextInt();????b?=?sc.nextInt();????c?=?sc.nextInt();????max?=a;????if(b>a&&b>c){????????max?=?b;????}????if(c>a&&c>b){????????max?=?c;????}????min?=?a;????if(b
5、輸入一個年份,判是閏年還是平年
public?class?IfTest5?{static?int?age;public?static?void?main(String[]?args)?{????System.out.println("輸入一個年份:");????Scanner?sc?=?new?Scanner(System.in);????age?=?sc.nextInt();????if(age%4==0||age%100==0||age%400==0){????????System.out.println(age+"是閏年?。。?!");????}else{????????System.out.println(age+"是平年?。。?!");????????}??????}?????}
以上就是長沙中公優(yōu)就業(yè)java培訓(xùn)機構(gòu)的小編針對“編程基礎(chǔ)之Java if條件語句練習(xí)題”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。