Java基础编程训练

案例一:买飞机票

需求:

  • 机票价格按照淡季旺季头等舱和经济舱收费,输入机票原价、月份和头等舱或经济舱
  • 机票优惠方案计算如下:旺季(5-10)头等舱9折,经济舱8.5折,淡季(11-来年4)头等舱7折,经济舱6,5折。
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
42
43
44
package eg1;
import java.util.Scanner;
public class eg1 {
public static void main(String[] args) {
System.out.println("请输入机票原价:");
Scanner sc = new Scanner(System.in);
double price = sc.nextDouble();
System.out.println("请输入月份:");
int month = sc.nextInt();
System.out.println("请输入舱位类型:");
String type = sc.next();
System.out.println("机票的价格是" + judge(price, month, type));
}

public static double judge(double price, int month, String type) {
if (month >= 5 && month <= 10) {
switch (type) {
case "头等舱":
price *= 0.9;
break;
case "经济舱":
price *= 0.85;
break;
default:
System.out.println("您的输入有误!");
break;
}
} else {
switch (type) {
case "头等舱":
price *= 0.7;
break;
case "经济舱":
price *= 0.65;
break;
default:
System.out.println("您的输入有误!");
break;
}
}
return price;
}
}

案例二:找素数

需求:

  • 找到101-200之间的素数,并打印出来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package eg2;

public class eg2 {
public static void main(String[] args){
for(int i=101;i<201;i++){
boolean flag=true;//信号位:标记
for(int j=2;j<i/2;j++){
if(i%j==0){
flag=false;
break;
}
}
//根据信号位判断是否输出这个数据
if(flag){
System.out.print(i+"\t");
}
}
}
}

案例三:开发验证码

需求:

  • 定义方法实现随机产生一个五位的验证码,每位可能是数字,大写字母,小写字母

分析:

  • 定义一个方法,生成验证码返回:方法参数是位数,返回值类型是String
  • 在方法内部使用for循环生成指定位数的随机字符,并连接起来
  • 把连接好的随机字符作为一组验证码进行返回
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
package eg3;
import java.util.Random;
public class eg3 {
public static void main(String[] args){
String code=createCode(5);
System.out.println("随机验证码:"+code);
}
public static String createCode(int n){
Random r=new Random();
String code="";//空字符
for(int i=0;i<n;i++){
//生成随机字符:大写,小写,数字(0 1 2)
int type=r.nextInt(3);
switch (type){
case 0:
//大写字母(A-Z)(65-65+25)
char ch=(char)(r.nextInt(26)+65);
code+=ch;
break;
case 1:
//小写字母(a-z)(97-97+25)
char ch1=(char)(r.nextInt(26)+97);
code+=ch1;
break;
case 2:
//数字
code+=r.nextInt(10);
break;
}
}
return code;
}
}

案例四:数组元素的复制

需求:

  • 把一个数组的元素复制到另外一个新的数组中去

分析:

  • 需要动态初始化一个数组,长度与原数组一样
  • 遍历数组的每一个元素,依次赋值给新数组
  • 输出两个数组的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package eg4;

public class eg4 {
public static void main(String[] args){
int[] arr1={11,22,33,44};
//int[] arr2=arr1并没有完成数组的复制
int[] arr2=new int[arr1.length];
copy(arr1,arr2);
printout(arr2);
}
public static void copy(int[] arr1,int[] arr2){
for(int i=0;i<arr1.length;i++){
arr2[i]=arr1[i];
}
}
public static void printout(int[] arr){
System.out.print("{");
for(int i=0;i<arr.length;i++){
System.out.print(i==arr.length-1?arr[i]:arr[i]+", ");
}
System.out.print("}");
}
}

案例五:评委打分

需求:

  • 六名评委打分,分数的范围【0-100】之间的整数,选手最后的得分为:去掉最高分和最低分后的四个评委的平均分,计算出选手的得分

分析:

  • 把六位评委的分录入到数组中去
  • 累加分数后减去最大值和最小值
  • 累加求和计算平均分
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
package eg5;
import java.util.Scanner;
public class eg5 {
public static void main(String[] args){
//6位评委分数的录入
int[] arr=new int[6];
input(arr);
System.out.print("平均分为:"+average(arr));
}
public static void input(int[] arr){
Scanner sc=new Scanner(System.in);
for(int i=0;i<arr.length;i++){
System.out.print("第"+(i+1)+"位评委的分数:");
arr[i]=sc.nextInt();
System.out.println();
}
}
public static double average(int[] arr){
int sum=0;
for(int i=0;i<arr.length;i++){
sum +=arr[i];
}
int max=arr[0];
int min=arr[0];
for(int i=0;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
}
if(arr[i]<min){
min=arr[i];
}
}
sum-=max;
sum-=min;
double average=sum/4.0;
return average;
}
}

案例六:模拟双色球系统

需求:

  • 业务分析、随机生成一组中奖号码

  • 用户输入一组双色球号码

  • 判断中奖情况

分析:

  • 中间号码由6个红球和一个蓝球组成(要求:6个红球要求不能重复)
  • 可以方法用于返回一组中奖号码(7个数据),返回的形式是一个整型数组
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package eg6;

import java.util.Random;
import java.util.Scanner;

public class eg6 {
public static void main(String[] args){
//生成幸运数字
int[] luck=createLuckynumber();
//用户输入一串数字
int[] user= userInput();
System.out.println("您投注的号码是:");
printArry(user);
System.out.println("中奖号码是:");
printArry(luck);
//判断中奖情况
judge(user,luck);
}
public static int[] createLuckynumber(){
//动态化生成7个位的数组
int[] numbers=new int[7];//[X,X,X,X,X,X,Y]
//遍历数组,为对应的每一个位置生成一个对应的号码
Random r=new Random();
for(int i=0;i<numbers.length-1;i++){
while(true){//利用死循环和标记位
boolean flag=true;
int data=r.nextInt(33)+1;
for(int j=0;j<i;j++){
if(numbers[j]==data){
flag=false;
break;
}
}
if(flag){
numbers[i]=data;
break;
}
}
}
//蓝球号
numbers[numbers.length-1]=r.nextInt(16)+1;
return numbers;
}
public static int[] userInput(){
int[] numbers=new int[7];
Scanner sc=new Scanner(System.in);
for(int i=0;i<numbers.length-1;i++){
System.out.println("请您输入第"+(i+1)+"个球号码:");
int data=sc.nextInt();
numbers[i]=data;
}
System.out.println("请您输入蓝球号码");
int data=sc.nextInt();
numbers[numbers.length-1]=data;
return numbers;
}
public static void printArry(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
}
System.out.println();
}
public static void judge(int[] one,int[] two){
int RedHit=0;
int BlueHit=0;
for(int i=0;i<one.length-1;i++){
for(int j=0;j<one.length-1;j++){
if(one[j]==two[j]){
RedHit++;
break;
}
}
}
BlueHit=one[6]==two[6]?1:0;
System.out.println("您命中了"+RedHit+"个红球");
System.out.println("您命中了"+BlueHit+"个蓝球");
if(BlueHit==1&&RedHit<3){
System.out.println("恭喜您,中了五元");
}
else if((BlueHit==0&&RedHit==4)||(BlueHit==1&&RedHit==3)){
System.out.println("恭喜您,中了十元");
}
else if(BlueHit==1&&RedHit==4){
System.out.println("恭喜您,中了200元");
}
else if((BlueHit==0&&RedHit==5)||(BlueHit==1&&RedHit==5)){
System.out.println("恭喜您,中了十3000元");
}
else if(BlueHit==0&&RedHit==6){
System.out.println("恭喜您,中了500万元");
}
else if(BlueHit==1&&RedHit==6){
System.out.println("恭喜您,中了1000万元");
}
else {
System.out.println("感谢您为福利事业做出的突出贡献!");
}
}
}