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; 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();
System.out.println("请您输入机票的月份(1-12)"); int month = sc.nextInt();
System.out.println("请您选择仓位类型:"); String type = sc.next();
System.out.println("优惠后的价格是:" + calc(money, month, type)); }
public static double calc(double money, int month, String type){
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 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; } }
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){ Random r = new Random(); String code = ""; for (int i = 0; i < n; i++){ int type = r.nextInt(3); switch (type){ case 0: char ch = (char)(r.nextInt(26) + 65); code = code + ch; break; case 1: char ch1 = (char)(r.nextInt(26) + 97); code += ch1; break; case 2: int num = r.nextInt(10); 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]; 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]; }
} }
|