Java Review

Demo1:猜数字小游戏

知识点:Random函数,Scanner函数,while循环,if,else if分支判断语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.itheima;
public class Procedure {
public static void main(String[] args) {

Random rd = new Random();
int a = rd.nextInt(100) + 1; // 定义变量a用于接收一个随机数范围在(0-99)+1
Scanner sc = new Scanner(System.in); //接收用户的输入
while (true){
int u = sc.nextInt();
System.out.println("请输入你的猜测");
if (a == u){
System.out.println("恭喜猜对!");
break;
} else if (a > u) {
System.out.println("小了,继续猜!");
}else {
System.out.println("大了,再试试!");
}
}
}

Demo2: 买飞机票

知识点:函数,Scanner函数,if分支语句嵌套switch,case,函数的参数和返回值。

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
package com.itheima;

import java.util.Scanner;

public class BuyPlaneTicket {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("请您输入机票原价:");
double money = sc.nextDouble(); // nextdouble用于接收用户输入的double型变量

System.out.println("请您输入机票的月份(1-12)");
int month = sc.nextInt(); // nextint用于接收用户输入的int类型变量

System.out.println("请您选择仓位类型:");
String type = sc.next(); //next()用于接收str类型变量

System.out.println("优惠后的价格是:" + calc(money, month, type)); // 调用函数输出优惠后的价格
}
/*
定义方法接收信息,并将计算后的价格返回
*/
public static double calc(double money, int month, String type){
/*
5-10月份是旺季(头等舱9折,经济舱8.5折),(11月到来年4月)淡季(头等舱7折经济舱6.5折)
*/
if (month >=5 && month <= 10){
//旺季
switch (type){
case "头等舱":
money *= 0.9;
break;
case "经济舱":
money *= 0.85;
break;
default:
System.out.println("您输入的仓位有误");
money = -1; //表示无法计算当前价格
}
} else if ((month == 11 && month == 12) || (month >=1 && month <=4)) {
//淡季
switch (type){
case "头等舱":
money *= 0.7;
break;
case "经济舱":
money *= 0.65;
break;
default:
System.out.println("您输入的仓位有误");
money = -1; //表示无法计算当前价格
}
}
return money;
}
}

Demo3:找到指定区间的素数(质数)

  • 质数的定义:除了自身和1再没有其他数能被其整除。

  • (质数的找法):判断该从0到此数的一半之间有没有一个数能除尽该数。伪代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    for int (j = 101; j < 201; j++){  //找从101-200之间的素数
    for(int i =2; j < i / 2; j++)
    {
    if (j % i == 0){
    //如果除尽表明不是质数,除不尽就是质数
    }
    }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int i = 101;i < 200; i++) {

boolean signal = true;

for (int j = 2; j< i / 2; j++) {

if (i % j == 0) {
signal = false; // 说明i不是素数
}
}

if (signal == true){
System.out.print(i + "\t");
}
}

Demo4:随机生成指定位数的验证码

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
package com.itheima;
import java.util.Random;
import java.util.Scanner;
public class Identify_Code {
public static void main(String[] args) {
System.out.println("请输入验证码的位数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String code = creatcode(n);
System.out.println("验证码为:" + code);
}
public static String creatcode(int n){
//定义一个for循环,循环n次,依次生成随机字符
Random r = new Random();
String code = ""; // 定义要生成的字符,初始为空
for (int i = 0; i < n; i++){
//通过random来确定到底来生成什么类型的字符
int type = r.nextInt(3); //type的取值为0-2
switch (type){
case 0:
//生成大写字母A 65 Z 65+25
char ch = (char)(r.nextInt(26) + 65); //0-25)+65
code = code + ch;
break;
case 1:
//生成小写字母a 97 z 97+25
char ch1 = (char)(r.nextInt(26) + 97); //0-25)+97
code += ch1;
break;
case 2:
//生成数字
int num = r.nextInt(10); //生成0-9之间的数字
code += num;
break;
}
}
return code;

}
}

Demo5:数组复制

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

public class CopyArray {
public static void main(String[] args) {
int[] array = {1, 33, 11, 12}; //定义数组
int[] array1 = new int[array.length]; // 定义空数组,长度与array相同
copy(array, array1); //调用赋值函数
for (int i = 0; i < array.length; i++){
System.out.print(array1[i] + "\t");
}
}
public static void copy(int[] arry, int[] arry1){
//给数组赋值
for (int i = 0; i < arry.length; i++){
arry1[i] = arry[i]; //将每一个元素赋值
}

}
}