案例一:判斷奇偶數(shù)
- 提示用戶鍵盤錄入一個數(shù)據(jù)并接收.
- 判斷該數(shù)據(jù)是奇數(shù)還是偶數(shù), 并將結(jié)果打印到控制臺上.
public class IfDemo03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入一個整數(shù): ");
int num = sc.nextInt();
if (num % 2 == 0) {
System.out.println(num + "是偶數(shù)");
} else {
System.out.println(num + "是奇數(shù)");
}
}
}
案例二:打印星期
用戶錄入[1, 7]之間的數(shù)字, 并接收,根據(jù)用戶錄入的數(shù)字, 打印對應(yīng)的星期, 格式如下:
用戶錄入1, 打印"星期一"
用戶錄入2, 打印"星期二"
...以此類推
用戶錄入非法數(shù)字, 打印"沒有這樣的日期"
import java.util.Scanner;
public class IfDemo04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入一個1 - 7之間的整數(shù): ");
int week = sc.nextInt();
if (week == 1) {
} else if(week == 2) {
System.out.println("星期二");
} else if(week == 3) {
System.out.println("星期三");
} else if(week == 4) {
System.out.println("星期四");
} else if(week == 5) {
System.out.println("星期五");
} else if(week == 6) {
System.out.println("星期六");
} else if(week == 7) {
System.out.println("星期日");
} else {
System.out.println("沒有這樣的日期, 你是從火星來的吧!");
}
}
}
案例三:發(fā)放獎勵
需求:小明快要期末考試了伟葫,小明爸爸對他說构舟,會根據(jù)他的考試成績,送他不同的禮物.禮物標(biāo)準(zhǔn)如下:
90~100 山地自行車一輛
80~89 游樂場玩一次
70~79 變形金剛玩具一個
70以下 胖揍一頓
import java.util.Scanner;
public class IfDemo05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入小明的考試成績: ");
int score = sc.nextInt();
if (score >= 95 && score <= 100) {
System.out.println("獎勵小明: 山地自行車一輛");
} else if(score >= 90 && score < 95) {
System.out.println("獎勵小明: 游樂場玩兒一次");
} else if(score >= 80 && score < 90) {
System.out.println("獎勵小明: 變形金剛玩具一個");
} else if(score >= 0 && score < 80){
System.out.println("獎勵小明: 男女雙混組合拳 + 掃帚棒法");
} else {
System.out.println("考試成績錄入有誤.");
}
}
}
案例三改:用switch語句求案例三
import java.util.Scanner;
public class SwitchDemo09 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入小明的考試成績: ");
int score = sc.nextInt();
if (score >= 0 && score <= 100) {
//合法成績
switch (score / 10) {
case 10:
case 9:
System.out.println("獎勵小明: 山地自行車一輛");
break;
case 8:
System.out.println("獎勵小明: 游樂場玩兒一次");
break;
case 7:
System.out.println("獎勵小明: 變形金剛玩具一個");
break;
default:
System.out.println("獎勵小明: 男女雙混組合拳 + 掃帚棒法");
break;
}
} else {
//非法成績
System.out.println("您錄入的成績有誤!");
}
}
}
案例四:獲取最大值
提示用戶錄入3個整數(shù), 并接收.
通過if語句獲取這三個整數(shù)的最大值.
將結(jié)果打印到控制臺上.
import java.util.Scanner;
public class IfDemo06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入第一個整數(shù): ");
int a = sc.nextInt();
System.out.println("請錄入第二個整數(shù): ");
int b = sc.nextInt();
System.out.println("請錄入第三個整數(shù): ");
int c = sc.nextInt();
//方式一: if嵌套實現(xiàn).
int max = a;
int max2 = a;
if (a > b) {
if (a > c) {
max = a;
} else {
max = c;
}
} else {
if(b > c) {
max = b;
} else {
max = c;
}
}
System.out.println("最大值: " + max);
//方式二: if. else.if語句實現(xiàn)
if (a > b && a > c) {
max2 = a;
} else if(b > a && b > c) {
max2 = b;
} else {
max2 = c;
}
System.out.println("最大值: " + max2);
}
}
案例五:春夏秋冬
一年有12個月, 分屬于春夏秋冬4個季節(jié), 鍵盤錄入一個月份, 請用程序?qū)崿F(xiàn)判斷該月份屬于哪個季節(jié), 并輸出旅东。
具體標(biāo)準(zhǔn)如下:
輸入: 1、2、12 輸出:冬季
輸入: 3瞬场、4、5 輸出:春季
輸入: 6涧郊、7贯被、8 輸出:夏季
輸入: 9、10底燎、11 輸出:秋季
輸入:其它數(shù)字 輸出:數(shù)字有誤
import java.util.Scanner;
public class SwitchDemo08 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入一個月份: ");
int month = sc.nextInt();
switch (month) {
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("沒有這樣的日期");
break;
}
}
}
案例六:漲工資
根據(jù)工齡(整數(shù))給員工漲工資(整數(shù)), 工齡和基本工資通過鍵盤錄入
漲工資的條件如下:
[10-15) +5000,
[5-10) +2500
[3~5) +1000
[1~3) +500
[0~1) +200
public static void main(String[] args) {
// 1.創(chuàng)建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
// 2.定義變量age表示工齡(假設(shè)為int類型)刃榨,定義變量salary表示基本工資(假設(shè)為int類型)
// int age,salary;
// 3.通過鍵盤錄入給工齡age賦值和基本工資salary賦值
// age = sc.nextInt();
// salary = sc.nextInt();
int age = sc.nextInt();
int salary = sc.nextInt();
int addSalary = 0;
// 4.對工齡進(jìn)行邏輯判斷,根據(jù)不同的工齡區(qū)間双仍,進(jìn)行漲工資salary+=n枢希;(n表示:漲工資的數(shù)量)
if(age>=15||age<0) {
System.out.println("您輸入的工齡不合法~");
return ;
}
//[10-15) +5000
if(age>=10&&age<15) {
addSalary = 5000;
} else if(age>=5&&age<10){//[5-10) +2500
addSalary = 2500;
} else if(age>=3&&age<5) {//[3~5) +1000
addSalary = 1000;
} else if(age>=1&&age<3) {//[1~3) +500
addSalary = 500;
} else if(age>=0&&age<1) {//[0~1) +200
addSalary = 200;
}
// 5.按照格式進(jìn)行打印
System.out.println("您目前工作了"+age+"年,基本工資為 "+salary+"元, 應(yīng)漲工資 "+addSalary+"元,漲后工資 "+(salary+addSalary)+"元");
}
案例七:水仙花數(shù)
鍵盤錄入一個三位數(shù)字,輸出該三位數(shù)字是否是水仙花數(shù)字?
水仙花數(shù)字要求: 指的是一個三位數(shù)朱沃,個位苞轿、十位茅诱、百位的數(shù)字立方和等于原數(shù)
例如 153 333 + 555 + 111 = 27 + 125 + 1 = 153
public static void main(String[] args) {
//1.創(chuàng)建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
//2.獲取一個int數(shù)字
System.out.println("請輸入一個三位整數(shù)");
int num = sc.nextInt();
//3.獲取個位,十位,百位
int ge = num%10;
int shi = num/10%10;
int bai = num/100%10;
//4.求個位,十位,百位的立方和
int sum = ge*ge*ge + shi*shi*shi + bai*bai*bai;
//5.利用if語句判斷立方和是否等于該數(shù)字本身,并輸出結(jié)果
if(sum == num) {
System.out.println(num+"是水仙花數(shù)字 ");
} else {
System.out.println(num+"不是水仙花數(shù)字 ");
}
}
輸入一個100-1000之內(nèi)的三位數(shù),求該數(shù)與100之間的水仙花數(shù)以及水仙花數(shù)的個數(shù)
public class Demo10 {
public static void main(String[] args) {
int ge, shi, bai,count = 0;
for (int i = 100; i < 1000; i++) {
ge = i / 1 % 10;
shi = i / 10 % 10;
bai = i / 100 % 10;
if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
System.out.println(i);
count++;
}
}
System.out.println("水仙花數(shù)的個數(shù)為: " + count);
}
}
案例八
求 1 ~ 100 之間的奇數(shù)和 ,以及奇數(shù)的個數(shù)
public class ForDemo07 {
public static void main(String[] args) {
int count = 0;
int sum = 0;
for (int i = 1; i <= 100 ; i++) {
if (i % 2 != 0) {
sum += i;
count++;
}
}
System.out.println("1-100之間的奇數(shù)個數(shù)為: " + count + ", 這些奇數(shù)的總和為: " + sum);
}
}
案例九
把 1~ 100 之間的數(shù)字 按照一行6個進(jìn)行輸出
public class Demo10 {
public static void main(String[] args) {
for (int i=1;i <= 100;i++){
System.out.print(i+" ");
if (i%6 == 0){
System.out.println();
}
}
}
}
案例十
按照從大到小的順序輸出個位+百位=十位+千位的數(shù)字及個數(shù);每行輸出5個滿足條件的數(shù)搬卒,之后用空格分隔
public class Demo10 {
public static void main(String[] args) {
int count = 0;
System.out.println("滿足條件的數(shù)為:");
for (int i = 9999;i >=1000;i--){
int ge = i%10;
int shi = i/10%10;
int bai = i/100%10;
int qian = i/1000;
if (ge + bai == shi + qian){
System.out.print(i+" ");
count++;
if (count%5 == 0){
System.out.println();
}
}
}
System.out.println("滿足條件的個數(shù)為"+count);
}
}
案例十一
錄入一個大于100的三位數(shù)瑟俭,求與100之間滿足各位數(shù)不為7,十位數(shù)不為5契邀,百位數(shù)不為3的數(shù)字之和
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個大于100的三位數(shù):");
int num = sc.nextInt();
int sum = 0;
for (int i=100;i<num;i++){
int ge = i%10;
int shi = i/10%10;
int bai = i/100;
if (ge != 7 || shi != 5 || bai !=3){
sum += i;
}
}
System.out.println("該數(shù)與100之間滿足條件的數(shù)字之和為:"+sum);