老丈人选女婿

程序要求:键盘录入女婿的酒量,如果大于2斤,老丈人给予回应,反之则不给予回应。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package hello;

import java.util.Scanner;

public class text4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入你的酒量!");
int Capacity = sc.nextInt();
if(Capacity>=2) {
System.out.println("小伙子不错哦!");
}else {
System.out.println("渣渣,喝不倒我就别想着穿我的小棉袄");
}
}
}

吃饭

程序要求:键盘录入一个整数,表示身上的钱。

如果大于等于100快,就吃网红餐厅。

否则,就吃经济实惠的沙县小吃。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package hello;

import java.util.Scanner;

public class text5 {
public static void main(String[] args) {
System.out.println("输入余额?决定今天吃饭方式!");
Scanner sc=new Scanner(System.in);
double balance= sc.nextInt();
if(balance>=100){
System.out.println("呀呼,网红餐厅吃大餐!!!");
}else{
System.out.println("害,经济实惠的沙县小吃也不错~");
}

}
}

商品付款

by 美国斯坦福大学Java入门练习

在实际开发中,如果要根据两种不同的情况来执行不同的代码,就需要用到if的第二种格式。

程序需求:

假设,用户在超市实际购买的商品价格总价为:600元。

键盘录入一个整数表示用户实际支付的钱。

如果付款大于等于600,表示付款成功,否则付款失败。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package hello;

import java.util.Scanner;

public class text6 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("输入你的付款数:");
int pay= sc.nextInt();
if(pay>=600){
System.out.println("付款成功");
}else {
System.out.println("付款失败");
}
}
}

影院选座

程序要求:

在实际开发中,

电影院选座也会使用到if判断。

假设某影院售卖了100张票,票的序号为1~100.

其中奇数票号做左侧,偶数票号坐右侧。

键盘录入一个整数表示电影票的票号。

根据不同情况,给出不同的提示:

如果票号为奇数,那么打印坐左边,如果票号为偶数,那么打印坐右边。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package hello;

import java.util.Scanner;

public class text7 {
public static void main(String[] args) {
System.out.println("请输入你的票号:");
Scanner sc=new Scanner(System.in);
byte ticket= sc.nextByte();
if(ticket%2!=0){
System.out.println("你的座位在左侧。");
}else {
System.out.println("你的座位在右侧。");
}
}
}

考试奖励

image-20230530090549426

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package hello;

import java.util.Scanner;

public class text8 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("考试分数:");
double score= sc.nextDouble();
if(score>95){
System.out.println("奖励自行车");
} else if (score>90) {
System.out.println("游乐场游玩一天");
} else if (score>80) {
System.out.println("奖励变形金刚一个");
}else{
System.out.println("奖励一顿藤条焖猪肉");
}

}
}

商品的价格

屏幕截图 2023-05-30 090851

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package hello;

import java.util.Scanner;

public class text9 {
public static void main(String[] args) {
System.out.println("输入商品总价:");
Scanner sc = new Scanner(System.in);
double pay = sc.nextDouble();
System.out.println("输入你的会员等级:");
byte VIP = sc.nextByte();
System.out.println("您需要支付的价格为:");
if (VIP == 3) {
pay = pay * 0.7;
System.out.println(pay);
} else if (VIP == 2) {
pay = pay * 0.8;
System.out.println(pay);
} else if (VIP == 1) {
pay = pay * 0.9;
System.out.println(pay);
} else {
System.out.println(pay);
}
}
}

运动计划

image-20230530163336752

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package hello;

import java.util.Scanner;

public class text10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入星期数:");
int eat = sc.nextInt();
System.out.println("你今天的运动方式为:");
switch (eat) {
case 1:
System.out.println("跑步");
break;
case 2:
System.out.println("游泳");
break;
case 3:
System.out.println("慢走");
break;
case 4:
System.out.println("动感单车");
break;
case 5:
System.out.println("拳击");
break;
case 6:
System.out.println("爬山");
break;
case 7:
System.out.println("好好吃一顿");
break;
default:
System.out.println("抱歉,你的输入不合法");
break;
}


}
}

休息日和工作日

image-20230530165200972

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package hello;

import java.util.Scanner;

public class text11 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入星期数");
int num = sc.nextInt();
switch (num) {
case 1, 2, 3, 4, 5 -> System.out.println("工作日");
case 6, 7 -> System.out.println("休息日");
default -> System.out.println("非法输入");
}
}
}

用户选择

image-20230530171019187

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package hello;

import java.util.Scanner;

public class text12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
int num = sc.nextInt();
switch (num){
case 1-> System.out.println("机票查询");
case 2-> System.out.println("机票预定");
case 3-> System.out.println("机票改签");
default -> System.out.println("退出服务");
}
}
}

更新于

请我喝[茶]~( ̄▽ ̄)~*

罗梓丰 微信支付

微信支付

罗梓丰 支付宝

支付宝