当前位置:   article > 正文

Java SE 小白学习笔记配套练习题_jcheng吧

jcheng吧

Java SE 配套习题

Day02

1,java数据类型有哪些?

​ 基本数据类型byte short int long float double Boolean char 除此之外还有N种引用数据类型

2,写出以下数据对应的数据类型?

​ 10 int
​ “-123” String
​ 1l long
​ 1f float
​ 1.1 double
​ 1 int
​ ‘1’ char
​ “1” String
​ true boolean

3,根据数据类型给出合适的值

​ byte 3
​ short 5
​ int 6
​ long 8L
​ float 2F
​ double 2.0
​ boolean true false
​ char ‘一个字母数字等等’
​ String “”
4,标识符可以使用(数字),(大写字母),(小写字母),除(_)($)以不外能使用(特殊字符),
不能使用(关键字)作为标识符
(数字)不能开头,不建议使用(汉字)

5,判断以下命名是否否和大驼峰命名法,并说明原因

​ HelloWorld √
​ Demo01 √
​ Person √
​ 1_Person
​ dog
​ Cat_01 √
6,判断以下命名是否否和小驼峰命名法,并说明原因
​ num
​ num01
​ age
​ name
​ password
​ username
​ password02 全对
7,什么是公共类?一个java文件中可以定义几个类,可以定义几个公共类 public class 多个 一个
8,编写一个程序输出1+2+“3”的结果 33
9,将字符a转换为数字并输出 System.out.println(0+‘a’);
10,将97转换为字符并输出 System.out.println((char)97);
(附加题)
11,计算自己姓名对应数字的和 System.out.println(‘张’+‘超’+‘超’);

Day03

2,键盘录入三个数,获取最小值打印输出

import java.util.Scanner;
//键盘录入三个数据,比较三个数据的最大值。
public class Demo05 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入数据1:");
		int a = scanner.nextInt();
		System.out.println("请输入数据2:");
		int b = scanner.nextInt();
		System.out.println("请输入数据3:");
		int c = scanner.nextInt();
		int m = a>b? a:b;
		int M = m>c? m:c;
		System.out.println("最大值为"+M);
		if(a>b) {
			if (a>c) {
				System.out.println(a);
			}else {
				System.out.println(c);
			}
		}else {
			if(b>c) {
				System.out.println(b);
			}else {
				System.out.println(c);
			}
		}
	}
}
  • 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

3,键盘录入账号密码,判断如果输入的账号为admin,输入的密码为123456,输出等于成功,否则输出等于失败

import java.util.Scanner;
public class Demo01{
	public static void main(String[] args){
		String admin="cc",password="16888";
		Scanner Sc = new Scanner(System.in);
		System.out.println("请输入你的账号:");
		String admin1 = Sc.next();
		System.out.println("请输入你的密码:");
		String password1 = Sc.next();
		//boolean x = admin1.equals(admin);
		//boolean y = password1.equals(password);
		//String TiShi = x==y==true?"正确":"错误";
		String TiShi = admin1.equals(admin)&&password1.equals(password)==true?"正确":"错误";
		System.out.println(TiShi);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4,将以下10进制的数转换为指定的进行
97 8进制 0141
12 2进制 0b1100
101 16进制 0x65
35 8进制 043
21 2进制 0b10101

Day04

2,将if嵌套的案例改为键盘录入完成

import java.util.Scanner;

/*
 * 需求:
 * 去超市购物,满200打8折,会员在打8折,不满200,是会员打8折,不满200,不是会员不打折
*/
public class Customer {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的物品实际金额:");
		int money = sc.nextInt();
		System.out.println("如果是该超市会员请输入1,否则输入0");
		int Vip = sc.nextInt();
		if(money<200 && money>=0) {
			if (Vip==1) {
				double a = money*0.8;
				double b = money-a;
				System.out.println("实际金额:"+money+"付款金额:"+String.format("%.2f", a)+"优惠金额:"+String.format("%.2f", b));
			}else {
				System.out.println(money);
			}
		}else if (money>=200) {
			if (Vip==1) {
				double a = money*0.8*0.8;
				double b = money-a;
				System.out.println("实际金额:"+money+"付款金额:"+String.format("%.2f", a)+"优惠金额:"+String.format("%.2f", b));
			}else {
				double a = money*0.8;
				double b = money-a;
				System.out.println("实际金额:"+money+"付款金额:"+String.format("%.2f", a)+"优惠金额:"+String.format("%.2f", b));
			}
		}
	}
}


//注:%.2f  %.表示 小数点前任意位数   2表示两位小数   格式后的结果为 f表示浮点型 
//String.format方法打印出的数据是字符串形式.
  • 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

4,完善小程序助你破产项目
思路:
1,欢迎来到小程序助你破产
2,提示用户可选择的节日有
1 元旦
2 春节
3 元宵

3,提示用户输入对应的节日编号
4,根本编号打印输出应该送的礼物,如果没有对应的节日,输出暂无录入该节日

import java.util.Scanner;
public class Festival {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("欢迎来到小程序助你破产");
		System.out.println("可供选择的节日有:\n1.春节;2.元旦;3.国庆;\n4.中秋;5.元宵;6.七夕");
		System.out.println("请输入对应节日的编号:");
		int tag = sc.nextInt();
		switch(tag){
			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;
			default:
			System.out.println("该节日目前没有开发");
			break;		
		}
	}
}
  • 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

5,快递员没有送货量,评级
0~50:开除
50~150:扣款
150~1000:正常薪资
1000~1500:奖励500
1500+:奖励1000

import java.util.Scanner;
public class Grande {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入每月送货量:");
		int sum = sc.nextInt();
		if (sum>=0 && sum<50) {
			System.out.println("开除");
		}else if (sum>=50 && sum<150) {
			System.out.println("扣款");
		}else if (sum>=150 && sum<1000) {
			System.out.println("正常薪资");
		}else if (sum>=1000 && sum<1500) {
			System.out.println("奖励500");
		}else if (sum>=1500) {
			System.out.println("奖励1000");
		}else {
			System.out.println("输入有误!!!");
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

6,将判断季节的案例改为if

import java.util.Scanner;
/*
 * 案例:输入月份显示对应的季节
		3,4,5月---->春季
		6,7,8月---->夏季
		9,10,11月--->秋季
		12,1,2---->冬季
 * */
public class Season {
	public static void main(String[] args) {
		Scanner sc =  new Scanner(System.in);
		System.out.println("请输入月份:");
		int month = sc.nextInt();
		if (month>=3 && month<=4) {
			System.out.println("春季");
		}else if (month>=6 && month<=8) {
			System.out.println("夏季");
		}else if (month>=9 && month<=11) {
			System.out.println("秋季");
		}else if (month==2 || month==12 || month==1) {
			System.out.println("冬季");
		}else {
			System.out.println("没有该月份,错误!!!");
		}
		switch (month) {
		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;
		case 12:
		case 1:
		case 2:
			System.out.println("冬季");
			break;
		default:
			break;
		}
	}
}
  • 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

Day05

1,计算1~100的所有数之和
思路:
1,定义变量记录已经计算的和
int sum = 0;
2,循环获取1~100之间的数
for(int num = 1; num < 101; num++){
3,使用该数字与已经计算的和相加
sum += num;
}
System.out.println(“1~100的和为:”+sum);
2,计算1100的偶数和与1100的奇数和

public class Demo01 {
	public static void main(String[] args) {
		int Onum = 0;
		int Jnum = 0;
		for (int i = 0; i <= 100; i++) {
			if (i%2 == 1) {
				Onum += i; 
			}else  {
				Jnum += i;
			}
		}
		System.out.println("100以内偶数和为"+Jnum+"100以内奇数和为:"+Onum);
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

4,计算10的阶乘

public class Demo01 {
	public static void main(String[] args) {
		int JCheng=1;
		for (int i = 1; i <= 10 ; i++) {
			JCheng = JCheng*i;
		}
		System.out.println("10的阶乘是:"+JCheng);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5,打印正方形

public class Demo08 {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6,打印空心正方形

public class Demo08 {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				if(i==0 || i==4 || j==0 || j==4){
					System.out.print("* ");
				}else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

7,打印三角形

public class Demo02 {
	public static void main(String[] args) {
		int a = 9;//菱形行数
		int b = (a+1)/2;//菱形最大行数
		
		//正三角形
		for (int i = 1; i <=b; i++) {
			for (int j = 0; j < b-i; j++) {
				System.out.print(" ");
			}
			for (int j = 0; j < 2*i-1; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
	//倒三角
		for (int k = b-1; k > 0; k--) {
			for (int j = 0; j <b-k ; j++) {
				System.out.print(" ");
			}
			for (int j = 0; j <2*k-1 ; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

  • 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

8,打印空心三角形

public class jj{
	public static void main(String[] args){
        for (int i = 1; i < 5; i++) {
			for (int j = 1; j < 5-i; j++) {
				System.out.print(" ");
			}
			for (int j = 1; j < 10; j++) {
				if (i==4 || j==1 || j==2*i+1) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

9,打印九九乘法表

public class Demo08 {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			for (int j = 1; j <=i; j++) {
				System.out.print(i+"*"+j+"="+i*j+"\t");
				
			}
			System.out.println();
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
附加题

生成6位验证码

六位验证码包含:
数字
小写字母
大写字母
思路:
1,定义一个变量记录已经生成的验证码 String
2,开启循环,循环次数为6.因为每次只能生成一位验证码
3,获取一位验证码
3.1:随机生成一个数字,用这个数字判断本次到底生成的是小写字母,大写字母还是数字
3.2:判断3.1生成的数字对应的到底是小写字母还是大写字母,或者是数字
3.2.1:生成小写字母
3.2.1.1随机生成一个0~25的数字+97
3.2.1.2:将3.2.1.1的数字强转为字符
3.2.1.3:将3.2.1.2生成的字符与步骤1的变量拼接
3.2.2:生成大写字母
3.2.2.1随机生成一个0~25的数字+65
3.2.2.2:将3.2.2.1的数字强转为字符
3.2.2.3:将3.2.2.2生成的字符与步骤1的变量拼接
3.2.3:生成数字
3.2.3.1随机生成一个0~9的数字
3.2.3.1将3.2.3.1生成的数字与步骤1的变量拼接
4,当循环结束后打印生成的验证码

import java.util.Random;
public class HomeWork {

	public static void main(String[] args) {
		String yanZheng = "";
		for (int i = 0; i < 6; i++) {
			Random random = new Random();
			int a = random.nextInt(3);
			if(a == 0) {
				int s1 = random.nextInt(10);
				yanZheng += s1;
			}else if (a == 1) {
				int s2 = random.nextInt(25) + 65;
				char s32 = (char)s2;
				yanZheng += s32;
			}else {
				int s3 = random.nextInt(25) + 97;
				char s31 = (char)s3;
				yanZheng += s31;
			}
		}
		System.out.println(yanZheng);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

周末01

1,重做试卷
2,jdk安装过程中安装了那些?

安装了JDK和jre

3,分别解释jdk,jre,jvm,gc,javase,javaee,javame名词含义

java的运行开发环境,Java运行环境,jvm垃圾回收机制,se是java的标准开发版本,ee是企业开发版本,
me是嵌入式开发版本
  • 1
  • 2

4,分别解释dos命令中cd,cls,exit,d:,…,.的作用

dir:查看目录内容 cd:打开指定目录 cls:清屏 exit:退出 d:进入d盘根目录 .是当前目录 ..是上一级目录
  • 1

5,分别解释javac,java命令的含义
6,java环境变量配置都有那些,变量值与变量名分别是什么?
7,java基本数据类型有哪些?
8,==与equals的区别
9,如何定义一个变量,如何给变量赋值
10,分支语句有哪些?语法格式是什么?
11,循环语句有哪些?语法格式是什么?
12,局部变量是什么?要注意什么?

13,模拟用户登录场景
要求:
1,给用户三次机会
2,如果用户在三次前登录成功,不需要在输入
3,如果在三次中登录成功,显示登录成功,如果登录失败显示登录失败
4,如果在三次登录中都没有成功,显示账号被锁
5,登录成功的账号为"admin",密码为:“jiushimima”;


package weekend01;
import java.util.Scanner;
public class Login {

	 public static void main(String[] args) {
		 
		String id = "admin";
		String password = "jiushimima";
		
		for (int j = 10; j > 0; j--) {			
			
			if(j == 6) {
				System.out.println("操作异常请30分钟后再试");
				break;
			}
			
			Scanner scanner = new Scanner(System.in);
			System.out.println("请输入你的账号:");
			String id1 = scanner.next();
			System.out.println("请输入你的密码:");
			String password01 = scanner.next();
					
			if(id.equals(id1)) {
				if(password.equals(password01) && id.equals(id1)) {
					System.out.println("登录成功!");
					break;
				}else {
					int a = j-8;
					System.out.println("密码错误!!!你还有"+a+"次机会!");
					if (j == 8) {
						System.out.println("密码输入太多账号被锁!!!");
						break;
					}
				}
			}else {
				System.out.println("该账号不存在,请仔细检查");
			}
		}			
	}
}

//增加功能随机验证码模块.(验证码正确才会进入数据匹配)
package weekend01;

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

public class Login02 {
	public static void main(String[] args) {
		String id = "admin";
		String password = "jiushimima";
		String yanZheng = "";
		
		X:for (int i = 10; i > 0; i--) {
			
			for (int j = 0; j < 4; j++) {
				Random random = new Random();
				int a = random.nextInt(3);
				if(a == 0) {
					int s1 = random.nextInt(10);
					yanZheng += s1;
				}else if (a == 1) {
					int s2 = random.nextInt(25) + 65;
					char s32 = (char)s2;
					yanZheng += s32;
				}else {
					int s3 = random.nextInt(25) + 97;
					char s31 = (char)s3;
					yanZheng += s31;
				}
			}
			
			Scanner scanner = new Scanner(System.in);
			System.out.println("请输入你的账号:");
			String id1 = scanner.next();
			System.out.println("请输入你的密码:");
			String password01 = scanner.next();
			System.out.println(yanZheng);
			System.out.println("请输入上面的随机验证码:");
			String yanZheng01 = scanner.next();
			
			if (yanZheng01.equals(yanZheng)) {
				
				for (int j = 10; j > 0; j--) {			
					
					if(i == 6) {
						System.out.println("操作异常请30分钟后再试");
						break X;
					}
							
					if(id.equals(id1)) {
						if(password.equals(password01)) {
							System.out.println("登录成功!");
							break X;
						}else {
							
							if (i == 8) {
								System.out.println("密码输入太多账号被锁!!!");
								break X;
							}else {
								int a = i- 8;
								System.out.println("密码错误!!!你还有"+a+"次机会");
								yanZheng = "";
								break;
							}
						}
					}else {
						System.out.println("该账号不存在,请仔细检查");
						yanZheng = "";
						break;
					}
				}		
				
			} else {
				if(i == 6) {
					System.out.println("验证码操作异常请30分钟后再试");
					break;
				}else {
					System.out.println("验证码不匹配!重新输入");
					yanZheng = "";	
				}				
			}
		}
	}
}
  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126

Day07

1,分别使用for与foreach遍历以下数组
String[] strs = {“java”,“javase”,“javaee”,“javame”,“jdk”,“jre”,“jvm”};
int[] nums = new int[]{8,6,4,9,88,886,999};

public static void a() {
		String[] strs = {"java","javase","javaee","javame","jdk","jre","jvm"};
		int[] nums = new int[]{8,6,4,9,88,886,999};
		for (int i : nums) {
			System.out.println(i);
		}
		for (String i : strs) {
			System.out.println(i);
		}
		
		for (int i = 0; i < nums.length; i++) {
			System.out.println(nums[i]);
		}
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2,设计二个方法,并调用
要求:第一个方法用于寻找strs数组中javase所在的下标
要求:第二个方法用于寻找520在数组nums中是否存在

/**
	 * 	设计二个方法,并调用
		要求:第一个方法用于寻找strs数组中javase所在的下标
		要求:第二个方法用于寻找520在数组nums中是否存在
	 */
	public static int b() {
		String[] strs = {"java","javase","javaee","javame","jdk","jre","jvm"};
		
		for (int i = 0; i < strs.length; i++) {
			
			if (strs[i].equals("javase")) {
				return i;
			}
		}
		return -1;
	}
	
	public static boolean c() {
		int[] nums = new int[]{8,6,4,9,88,886,999};
		for (int i = 0; i < nums.length; i++) {
			if (520 == nums[i]) {
				return true;
			}		
		}
		return false;
	}
  • 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

3,随机生成7个数,取值区间为1~31,存储到数组中.让用户输入幸运数字,如果用户输入一个幸运数字,在数组中存在,提示用户中奖,别墅靠海.否则告诉用户谢谢惠顾

/**
 * 随机生成7个数,取值区间为1~31,存储到数组中.让用户输入幸运数字,如果用户输入一个幸运数字
 * ,在数组中存在,提示用户中奖,别墅靠海.否则告诉用户谢谢惠顾
 */
import java.util.Random;
import java.util.Scanner;

public class Demo04 {
	public static void main(String[] args) {
		
		int[] num01 = d();//b方法中的返回值接收
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入你的幸运数字:");
		int a = scanner.nextInt();
		
		System.out.println("中奖的号码是:");
		for (int i : num01) {
			System.out.print(i+",");
		}
		System.out.println();
		
		if(b(num01, a)) {
			System.out.println("别墅靠海");
		}else {
			System.out.println("用户谢谢惠顾");
		}
		
	}
	
	//随机生成7位不重复的数,存储到数组中并返回
	public static int[] d() {
		int[] num = new int[7];
		Random random = new Random();

		for (int i = 0; i < num.length; i++) {
			int nums = random.nextInt(31) + 1;
			if (b(num, nums)) {
				i--;// 已经存在时,重新循环
			} else {
				num[i] = nums;// 不存在时,赋值
			}
		}
		return num;
	}

	//判断数组中是否该元素
	public static boolean b(int[] n, int i) {
		for (int j : n) {
			if (i == j) {
				return true;
			}
		}
		return false;
	}
}
  • 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

彩票系统(*)

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

public class Demo03 {

	public static void main(String[] args) {
		int[] num = a();// 把a,d方法中的返回值接收
		int[] num01 = d();
		int a = c(num,num01);//获取中奖个数
		
		System.out.println("您输入的号码是:");
		for (int i : num) {
			System.out.print(i+",");
		}
		System.out.println();
		
		System.out.println("中奖的号码是:");
		for (int i : num01) {
			System.out.print(i+",");
		}
		System.out.println();
		
		//判断中奖个数
		switch (a) {
		case 7:
			System.out.println("一等奖,别墅靠海");
			break;
		case 6:
			System.out.println("二等奖,500万");
			break;
		case 5:
			System.out.println("三等奖,心系天下");
			break;
		case 4:
			System.out.println("四等奖,吃火锅");
			break;
		case 3:
			System.out.println("五等奖,泡面一桶");
			break;

		default:
			System.out.println("谢谢惠顾,感谢您为福利事业做出贡献,本次中了"+a+"数");
			break;
		}
	}
	
	//随机生成7位不重复的数,存储到数组中并返回
	public static int[] d() {
		int[] num = new int[7];
		Random random = new Random();

		for (int i = 0; i < num.length; i++) {
			int nums = random.nextInt(31) + 1;
			if (b(num, nums)) {
				i--;// 已经存在时,重新循环
			} else {
				num[i] = nums;// 不存在时,赋值
			}
		}
		return num;
	}

	//判断用户输入的7位数字合理,并存入一个数组
	public static int[] a() {
		int nums01 ;
		int[] num01 = new int[7];
		Scanner scanner = new Scanner(System.in);
		for (int i = 0; i < 7; i++) {
			System.out.println("请输入(1~31)你的第"+(i+1)+"位幸运数字");
			nums01 = scanner.nextInt();
			if(nums01>31 || nums01<1) {
				System.out.println("输入数字超出范围请重新输入");
				i--;
			}else if (b(num01, nums01)) {
				System.out.println("你已输入以上数字请重新输入");
				i--;
			}else {
				num01[i] = nums01; 
			}
		}
		return num01;
	}
	
	
//	// 判断两数组中的想同元素,返回相同个数
//	public static int c() {
//		
//		int[] num2 = a();// 把a,b方法中的返回值接收//该方法错误,调用a();b();会重新生成随机7个号码,也会让用户一直重新输入
//		int[] num02 = d();
//		int b = 0; // 记录两数组元素相同的个数
//		for (int i = 0; i < 7; i++) {
//			for (int j = 0; j < 7; j++) {
//				if (num02[i] == num2[j]) {
//					b++;
//				}
//			}
//		}
//		return b;
//	}
	// 判断两数组中的想同元素,返回相同个数
	public static int c(int[] n,int[] m) {
		
		int b = 0; // 记录两数组元素相同的个数
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 7; j++) {
				if (n[i] == m[j]) {
					b++;
				}
			}
		}
		return b;
	}
	
	//判断数组中是否该元素
	public static boolean b(int[] n, int i) {
		for (int j : n) {
			if (i == j) {
				return true;
			}
		}
		return false;
	}
}
  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123

Day08

查找二维数组中一维数组的值是否存在

//查找二维数组中一维数组的值是否存在
	public static boolean find(int[][] nums,int a) {
		
		for (int i = 0; i < nums.length; i++) {
			for (int j = 0; j < nums[i].length; j++) {
				if ( a == nums[i][j]) {
					return true;
				}
			}
		}
		return false;
	}
	/**
	 * 
	 * @param nums //传入的二维数组
	 * @param a  //是否存在的值
	 * @return
	 */
	//查找二维数组中一维数组的值是否存在
		public static boolean find01(int[][] nums,int a) {
			
			for (int[] i : nums) {
				for (int js : i) {
					if (js == a) {
						return true;
					}
				}
			}
			return false;
		}
  • 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

查找二维数组中一维数组值对应的下标

/**
	 * 查找该值在二维数组中是否存在,若存在打印该值在二维数组的下标
	 * @param nums 	//需要查找的二维数组
	 * @param a		//查找的值
	 */
	public static void find02(int[][] nums, int a) {

		for (int i = 0; i < nums.length; i++) {
			for (int j = 0; j < nums[i].length; j++) {
				if (a == nums[i][j]) {
					System.out.println("该数在二维数组中的一维数组的下标为" + i + ",在该数组的第" + j + "位");
					return;
				}
			}
		}
		System.out.println("该数在二维数组中不存在");
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

杨辉三角(了解)

public class YanHuiSanJiao {
	
	public static void main(String[] args) {
		int[][] nums;
		nums = name();
		for (int[] is : nums) {
			for (int is2 : is) {
				System.out.print(is2+"\t");
			}
			System.out.println();
		}
	}
	
	public static int[][] name() {
		int[][] num = new int[7][7]; 
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 7; j++) {
				if(j == 0) {
					num[i][j] = 1;
				}else if(i>0 && j>0){
					num[i][j] = num[i-1][j] + num[i-1][j-1];
				}
			}
		}
		return num;
	}
}

  • 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

Day09

第一题

/**
1,创建一个Dog类
	public class Dog{}

2,在Dog类中声明以下属性
	姓名
	性别
	年龄
	毛色
3,在Dog类中定狗叫的方法
	调用方法时会打印 谁谁谁:汪汪汪
4,在Dog类中调用狗吃饭的方法
	要求:传入食物名称
5,创建Dog对象时,必须给Dog的对象的属性赋值
*/
public class Dog {
	public int age ;
	public String name;
	public String sex;
	public String colour;
	
	public Dog(int age, String name, String sex, String colour) {
		this.age = age;
		this.name = name;
		this.sex = sex;
		this.colour = colour;
	}
	
	public void call() {
		System.out.println(name+":wangwangwang");
	}
	public void eat(String name) {
		System.out.println(this.name+"吃"+name);
	}
	
}
/**
6,创建一个Dog对象,名字为富贵,公,2,黄色
7,在创建一个Dog对象,名字为旺财,母,3,黑色
8,输出步骤6创建的狗对象的名称
9,将步骤7创建的对象名称改为来福
10,让步骤6的对象叫,让步骤7的对象吃
*/

public class Demo01 {
	
	public static void main(String[] args) {
		
		Dog dog1 = new Dog(3, "zq", "公", "土黄色");
		Dog dog2 = new Dog(3, "kmk", "母", "土狗色");
		
		System.out.println(dog1.name);
		dog2.name = "qz";
		dog1.call();
		dog2.eat("骨头");
		
	}	
}
  • 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

第二题

/**
 *  1,创建猫的以下对象
		喵喵,白色,2,雌
		布丁,黑色,1,公
	2,让布丁叫喵喵喵
	3,修改喵喵名字为翠花,获取布丁的名字打印输出
	4,创建老鼠对象
		碎怂,灰
	5,创建狗对象
		老皮
	6,老皮抓碎怂,碎怂说:老皮,你给我等着
	7,翠花说老皮你多管闲事
 */

package Day09;

public class Demo02 {

	public static void main(String[] args) {
		 
		Cat cat1 = new Cat("miaomaio", "白色",2, "雌");
		Cat cat2 = new Cat("布丁", "褐色", 1, "公");
		
		cat2.call();
		cat1.name = "翠花";
		
        System.out.println(cat2.name);
        
		Mouse mouse = new Mouse("碎怂", "灰");
		
		Dog dog = new Dog("老皮");
		
		dog.zhua(mouse);
		
		cat1.say(dog);
	}

}

class Cat{
	public String name;
	public String colour;
	public int age;
	public String sex;
	
	
	
	public Cat(String name, String colour, int age, String sex) {
		this.name = name;
		this.colour = colour;
		this.age = age;
		this.sex = sex;
	}

	public void call() {
		System.out.println(name+":miaomiaomiao");
	}
	
	public void say(Dog dog ) {
		System.out.println(name+"说"+dog.name+"你多管闲事");
	}
}

class Mouse{
	public String name;
	public String colour;
	public Mouse(String name, String colour) {
		this.name = name;
		this.colour = colour;
	}	
	
}

class Dog{
	public String name;
	
	public Dog(String name) {
		this.name = name;	
	}

	public void zhua(Mouse mouse) {
		System.out.println(name+"抓"+mouse.name+","+mouse.name+"说:"+name+",你给我等着");
	}

//	应该让cat说不应该是dog    
//	public void see(Cat cat) {
//		System.out.println(cat.name+"说"+name+"你多管闲事");
//	}
}

  • 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

Day10

1,创建人,狗,猫对象如下
人:
富贵 18 男
来福 38 女
狗:
旺财 雌
猫:
布丁 雌
要求:使用继承关系,并使用super完成构造函数
2,在人,猫,狗中定义吃饭的方法
人吃火锅
猫吃老鼠
狗吃米田共
要求:重写父类方法
3,将富贵转换为父类对象
4,将3中的父类对象转换为人的对象

package Day10;

//测试类
public class Test {
	public static void main(String[] args) {
		Person p1 = new Person("富贵", "男", 18);
		Person p2 = new Person("来福", "女", 38);
		
		Dog dog = new Dog("旺财", "雌");
		
		Cat cat  = new Cat("布丁", "雌");
		
		p2.eat();
		dog.eat();
		cat.eat();
		
		Animal animal = p1;
		
		Person p3 = (Person)animal;
		
		System.out.println(p3 == p1);
		
		p3.eat();
	}
}

//动物类
public class Animal {
	private String name;
	private String sex;
	
	public Animal() {
		super();
	}
	
	public Animal(String name,String sex) {
		this.name = name;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public void eat() {
		System.out.println("该父类方法没有被定义");
	}
}


//人类
public class Person extends Animal{
	public int age;
	
	public Person() {
		super();
	}
	public Person(String name,String sex,int age) {
		super(name,sex);
		this.age = age;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void eat() {
		System.out.println(getName()+":吃火锅");
	}

}


//猫类
public class Cat extends Animal{
	
	public Cat() {
		super();
	}
	public Cat(String name,String sex) {
		super(name,sex);
	} 

	public void eat() {
		System.out.println(getName()+":吃老鼠");
	}
}


//狗类
public class Dog extends Animal{

	public Dog() {
		super();
	}
	public Dog(String name,String sex) {
		super(name,sex);
	} 
	public void eat() {
		System.out.println(getName()+":吃张强");
	}
}
  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

Day12

第一题

package Day12;
/**1.鸟会飞,飞机会飞,超人会飞
   请描述以上类
   */
public interface Fly {
	void fly();
}

public class Bird implements Fly{
	private String name;

	public Bird() {
		super();
	}

	public Bird(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void fly() {
		System.out.println("鸟会飞");
	}
}

public class Superman implements Fly{

	private String name ;
	private String sex;
	private int age;
	
	public Superman() {
		super();
	}

	public Superman(String name, String sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public void fly() {
		System.out.println("超人会飞");		
	}
}

public class Plane implements Fly{

	private String  modle;
	private String colour;
	private String company;
	
	public Plane() {
		super();
	}

	public Plane(String modle, String colour, String company) {
		super();
		this.modle = modle;
		this.colour = colour;
		this.company = company;
	}

	public String getModle() {
		return modle;
	}

	public void setModle(String modle) {
		this.modle = modle;
	}

	public String getColour() {
		return colour;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	@Override
	public void fly() {
		
		System.out.println("飞机会飞");
	}
}

  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

第二题

package Day12;
/**
 * 人有一个抽烟的方法,需要传入点火的对象
	打火机可以点火
	超人可以点火
	煤气灶可以点火
 * @author z'c'c
 *
 */
public class Test {
	public static void main(String[] args) {
		Person person = new Person("张强");
		person.somking(new Superman());
	}
}

//接口点火功能
public interface IgnitionInterface {
	void Ignition();
}

//打火机点火
public class DaHJ implements IgnitionInterface{
	
	@Override
	public void Ignition() {
		System.out.println("打火机点火");
		
	}
}

//煤气灶点火
public class MeiQZ implements IgnitionInterface{

	public void Ignition() {
		System.out.println("煤气灶点火");
		
	}
}
  • 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

第三题

//电脑连接鼠标键盘等案例,要求自己完成u盘,扩展器
package Day13;
public class Computer {

	private USBInterface usb01;
	private USBInterface usb02;
	private USBInterface usb03;
	private boolean isOpen = false;
	public Computer() {
		super();
	}
	
	public void setUsb01(USBInterface usb01) {
		if(this.usb01 != null && this.isOpen) {
			this.usb01.close();
		}
		
		this.usb01 = usb01;
		if(this.isOpen) {
			this.usb01.conn();
		}
	}
	public void setUsb02(USBInterface usb02) {
		if(this.usb02 != null && this.isOpen) {
			this.usb02.close();
		}
		
		this.usb02 = usb02;
		if(this.isOpen) {
			this.usb02.conn();
		}
	}
	public void setUsb03(USBInterface usb03) {
		if(this.usb03 != null && this.isOpen) {
			this.usb03.close();
		}
		
		this.usb03 = usb03;
		if(this.isOpen) {
			this.usb03.conn();
		}
	}
	
	public void open() {
		System.out.println("准备开机中");
		if (this.usb01 != null) {
			this.usb01.conn();
		}

		if (this.usb02 != null) {
			this.usb02.conn();
		}

		if (this.usb03 != null) {
			this.usb03.conn();
		}

		this.isOpen = true;
		System.out.println("开机成功");
	}
	public void off() {
        System.out.println("准备关机中");
        if (this.usb01 != null) {
            this.usb01.close();
        }

        if (this.usb02 != null) {
            this.usb02.close();
        }

        if (this.usb03 != null) {
            this.usb03.close();
        }

        this.isOpen = false;
        System.out.println("已经关机");
    }
}


//usb接口
package Day13;
public interface USBInterface {
	void conn();
	void close();
}


//扩展器
package Day13;
public class USBKuo implements USBInterface{
	private USBInterface usb01;
	private USBInterface usb02;
	private USBInterface usb03;
	private boolean isOpen = false;
	
	public void setUsb01(USBInterface usb01) {
		if(this.usb01 != null && this.isOpen) {
			this.usb01.close();
		}
		
		this.usb01 = usb01;
		if(this.isOpen) {
			this.usb01.conn();
		}
	}
	public void setUsb02(USBInterface usb02) {
		if(this.usb02 != null && this.isOpen) {
			this.usb02.close();
		}
		
		this.usb02 = usb02;
		if(this.isOpen) {
			this.usb02.conn();
		}
	}
	public void setUsb03(USBInterface usb03) {
		if(this.usb03 != null && this.isOpen) {
			this.usb03.close();
		}
		
		this.usb03 = usb03;
		if(this.isOpen) {
			this.usb03.conn();
		}
	}
	
	@Override
	public void conn() {
		
		if (this.usb01 != null) {
			this.usb01.conn();
		}

		if (this.usb02 != null) {
			this.usb02.conn();
		}

		if (this.usb03 != null) {
			this.usb03.conn();
		}

		this.isOpen = true;
		System.out.println("扩展器已连接");
	}

	@Override
	public void close() {
		if (this.usb01 != null) {
            this.usb01.close();
        }

        if (this.usb02 != null) {
            this.usb02.close();
        }

        if (this.usb03 != null) {
            this.usb03.close();
        }

        this.isOpen = false;
		System.out.println("扩展器断开连接");		
	}
}

package Day13;
public class Mouse implements USBInterface{

	@Override
	public void conn() {
		System.out.println("鼠标已经连接");	
	}

	@Override
	public void close() {
		System.out.println("鼠标断开连接");
	}	 
}

package Day13;
public class Key implements USBInterface{

	@Override
	public void conn() {
		System.out.println("键盘已连接");
		
	}

	@Override
	public void close() {
		System.out.println("键盘断开连接");		
	}
}

  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194

第四题

按钮

package Day14;
public class Test02 implements OnClickListener{
	public static void main(String[] args) {
		Test02 listener = new Test02();
		Button button = new Button();
		button.setListener(listener);
		System.out.println("//");
		button.down();
	}
	@Override
	public void click() {
		// TODO Auto-generated method stub
		System.out.println("欢迎光临");
	}
}


public class Button {
	private OnClickListener listener;

	public void setListener(OnClickListener listener) {
		this.listener = listener;
	}
	
	public void down() {
		listener.click();
	}	
}

public interface OnClickListener {
	void click();
}
  • 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

第五题

台灯(接口回调)

package weekend02;
/*
 *  设计一个台灯类(Lamp)其中台灯有灯泡类(Buble)这个属性,还有开灯(on)这个方法。
   	设计一个灯泡类(Buble),灯泡类有发亮的方法,其中有红灯泡类(RedBuble)和绿灯泡类(GreenBuble)
   	他们都继承灯泡类(Buble)一个发亮的方法。
 */
public class Test {

	public static void main(String[] args) {
		Lamp lamp = new Lamp();
		RedBuble redBuble = new RedBuble();
		lamp.on(redBuble);
			
		GreenBuble greenBuble = new GreenBuble();
		lamp.on(greenBuble);
	}
}
public class Buble {
	public void FaLiang() {
		System.out.println("发亮方法");
	}
}
public class RedBuble extends Buble {
	public void FaLiang() {
		System.out.println("发红光");
	}
}
public class GreenBuble extends Buble{
	public void FaLiang() {
		System.out.println("发绿光");
	}
}

  • 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

银行业务(综合题)

4.银行业务
用户类
属性:
昵称
账号
密码
余额
构造函数
get与set方法
银行类
属性:
昵称
存储用户集合
用户数量
构造函数,传入昵称
登录
注册
存钱
取钱
查询余额
环境类
创建银行
开启死循环
欢迎
提示可选项,登录,注册 一级菜单操作
如果登录成功
死循环包裹
存钱,取钱,查询余额,退出 二级菜单

package Day16;

import java.util.Scanner;

public class TestBank {
	public static void main(String[] args) {
		Bank bank = new Bank("张氏银行");
		while (true) {
			System.out.println(bank.getName() + "欢迎您");
			one(bank);
		}
		
	}
	
	//一级菜单
	@SuppressWarnings("resource")
	public static void one(Bank bank) {
		System.out.println("欢迎你,可选择的操作有:\n1.注册\n2.登录");
		System.out.println("请输入你需要的选择编码");
		Scanner scanner = new Scanner(System.in);
		String tag = scanner.next();//没有用int类型接收,而是用String提供的hashCode获得1.2的哈希码值
		switch (tag.hashCode()) {
		case 49:
			if (tag.equals("1")) {
				bank.regist();
				return;
			}
			break;
		case 50:
			if (tag.equals("2")) {
				User user = bank.login();
				if(user != null) {
					two(bank, user);
					}
				return;
			}
			break;
		}
		System.out.println("输入有误,请重新输入");
	}
	
	//	死循环包裹 存钱,取钱,查询余额,退出	
	//二级菜单
	public static void two(Bank bank, User user) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("欢迎你,可选择的操作有:\n1.存钱\n2.取钱\n3.查询余额\n4.退出操作");
			System.out.println("请输入你需要的选择编码");
			int tag = scanner.nextInt();
			if (tag == 1) {
				bank.saveMoney(user);
			}else if (tag == 2) {
				bank.getMoney(user);
			}else if (tag == 3) {
				bank.lookMoney(user);
			}else if(tag == 4){
				return;
			}else {
				System.out.println("选择有误请重新输入");
			}
			
		}
	}
}

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

public class Bank {
	private String name;
	private static ArrayList<User> users = new ArrayList<User>();
	
	public Bank() {
		super();
	}
	public Bank(String name) {
		super();
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	//注册
	public boolean regist() {
		System.out.println("欢迎来到张氏银行!!!");
		System.out.println("请输入你的昵称");
		Scanner scanner = new Scanner(System.in);
		String name = scanner.next();
		System.out.println("请输入你的账号");
		String userName = scanner.next();
		System.out.println("请输入输入你的密码");
		String password01 = scanner.next();
		System.out.println("请再一次输入你的密码");
		String password02 = scanner.next();
		
		boolean isSave = isSave(userName);
		if (isSave) {
			System.out.println("该用户已经存在请重新输入");
			return false;
		}
		if (!password01.equals(password02)) {
			System.out.println("两次输入密码不一致,请重新创建");
			return false;
		}
		User user = new User(name, userName, password01, 0);
		users.add(user);
		System.out.println("恭喜您!!!用户创建success");
		return true;	
	}
	//登录
	public User login() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入你的账号");
		String username = scanner.next();
		System.out.println("请输入你的密码");
		String password = scanner.next();
		String i = Suiji();
		System.out.println(i);
		System.out.println("请输入上述随机验证码(不区分大小写)");
		String m = scanner.next();
		if (users != null) {
			for (User user : users) {
				if (user != null && 
						user.getUserName().equals(username) && 
						user.getPassword().equals(password) && 
						i.equalsIgnoreCase(m)) {
					System.out.println("登录成功");
					return user;
				}
			}
		}
		System.out.println("登录失败");
		return null;
	}
	
	/**
	 * 判断该用户是否已经创建
	 * @param username
	 * @return true为已经存在
	 */
	public boolean isSave(String username) {
		if (users != null ) {
			//集合遍历会产生空指针异常
			for (User user :  users) {
				//刚开始把判断放前面,user可能为空调用getUserName()会产生空指针异常
				if (user != null && username.equals(user.getUserName()) ) {
					return true ;
				}
			}
		}
		return false;
	}
	//生成四位随机数
	public String Suiji() {
		String i="";
		Random random = new Random();
		for (int j = 0; j < 4; j++) {
			int s = 0;
			int m = random.nextInt(3);
			switch (m) {
			case 0:
				 s = random.nextInt(10);
				 i += s + "";
				break;
			case 1:
				 s = random.nextInt(25)+65;
				 i += (char)s;
				break;
			case 2:
				 s = random.nextInt(25)+97;
				 i += (char)s;
				break;
			default:
				break;
			}	
		}
		return i;
	}
	
	//存钱
	public void saveMoney(User user) {
		System.out.println("请输入你本次的存款金额");
		Scanner scanner = new Scanner(System.in);
		int money = scanner.nextInt();
		if (money > 0) {
			int oldMoney = user.getMoney();
			int newMoney = oldMoney + money;
			user.setMoney(newMoney);
			System.out.println("原余额为:"+oldMoney+"元\n本次存入:"+money+"元\n当前余额为:"+newMoney);
		} else {
			System.out.println("存入金额有误");
		}
	}
	
	//取钱
	public void getMoney(User user) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入您本次的取款金额");
		int money = scanner.nextInt();
		int oldMoney = user.getMoney();
		if (money>0 && money < oldMoney) {
			int newMoney = oldMoney - money;
			user.setMoney(newMoney);
			System.out.println("原余额为:"+oldMoney+"元\n本次取款:"+money+"元\n当前余额为:"+newMoney);
		}else if (money>0 && money > oldMoney) {
			System.out.println("当前余额不足");
		}else {
			System.out.println("取款金额有误");
		}
	}
	
	//查询余额
	public void lookMoney(User user) {
		int money = user.getMoney();
		System.out.println("当前账户余额为:"+money+"元");
	}
}
public class User {
	private String name;
	private String userName;
	private String password;
	private int money;
	public User() {
		super();
	}
	public User(String name, String userName, String password, int money) {
		super();
		this.name = name;
		this.userName = userName;
		this.password = password;
		this.money = money;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	
}
  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262

Day13

第一题

package Day13;
/**
 * 	1,创建一个Dog类,该类的属性有,姓名,性别,类型,颜色,年龄.要求写成标准类
 	2,创建Dog类的对象,并打印输出
		1,旺财,公,中华田园犬,黄,10
		2,富贵,雌,金毛,黄,8
		3,旺财,公,中华田园犬,黄,5
	3,判断Dog对象是否一致,要求除年龄外,其他属性值都相同就认为是同一只狗
	4,将Dog对象存储到数组中
 * @author z'c'c
 *
 */
public class Test {
	public static void main(String[] args) {
		Dog dog1 = new Dog("旺财","公","中华田园犬","黄色",10);
		Dog dog2 = new Dog("富贵","雌","金毛","金色",8);
		Dog dog3 = new Dog("旺财","公","中华田园犬","黄色",5);
		
		estimate(dog1, dog3);
		System.out.println(dog1.toString());
		System.out.println(dog3.toString());
		dog1.equals(dog3);
	
		Dog[] dogs = {dog1,dog2,dog3};
        for (Dog dog : dogs) {
			System.out.println(dog);
		}
		
	}

	public static void estimate(Dog dog01,Dog dog02) {
		
		if(dog01.getName().equals(dog02.getName()) && dog01.getSex().equals(dog02.getSex()) 
			&&	dog01.getColour().equals(dog02.getColour()) 
			&& dog01.getLeixing().equals(dog02.getLeixing())) {
			System.out.println("这是一只狗!!!");
			return;
		}
		System.out.println("这不是一只狗");
	}
	
}

package Day13;
/*
 * 该类的属性有,姓名,性别,类型,颜色,年龄.要求写成标准类
 */
public class Dog {
	private String name;
	private String sex;
	private String leixing;
	private String colour;
	private int age;
	
	public Dog() {
		super();
	}

	public Dog(String name, String sex, String leixing, String colour, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.leixing = leixing;
		this.colour = colour;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getLeixing() {
		return leixing;
	}

	public void setLeixing(String leixing) {
		this.leixing = leixing;
	}

	public String getColour() {
		return colour;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Dog [name=" + name + ", sex=" + sex + ", leixing=" + leixing + ", colour=" + colour + ", age=" + age
				+ "]";
	}
	
	@Override
		public boolean equals(Object obj) {
			if(this == obj ) {
				System.out.println("这是一只狗!!!");
				return true;
			}
			if(obj instanceof Dog) {
				Dog dog = (Dog)obj;
				if(name.equals(dog.name) && sex.equals(dog.sex) 
						&&	colour.equals(dog.colour) 
						&& leixing.equals(dog.leixing)) {
						System.out.println("这是一只狗!!!");
						return true;
			}
				
		}
			System.out.println("这不是一只狗");
			return false;
	}
}

  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134

内部类与接口回调

按钮案例用匿名内部类方式写出

public class Test02{
	public static void main(String[] args) {
		Button button = new Button();
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void click() {
				// TODO Auto-generated method stub
				System.out.println("欢迎光临");
			}
		};//匿名内部类实现接口回调
		
		button.setListener(onClickListener);
		System.out.println("//");
		button.down();
	}
}
public class Button {
	private OnClickListener listener;

	public void setListener(OnClickListener listener) {
		this.listener = listener;
	}
	
	public void down() {
		listener.click();
	}	
}
public interface OnClickListener {
	void click();
}
  • 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

Day14

package Day14;
/*
 * 让用户账号密码,判断账号密码是否匹配
	要求:
		账号,密码忽略前后空白
		长度要求在6~14位之间,如果超出提示不符合规则
		比较账号是忽略大小写比较
		比较密码不忽略大小写比较
 */
public class Test {

	public static void main(String[] args) {
		
		String userName = " fdf fds ";
		userName = userName.trim();
		String userPassword = "  A1131 45664 ";
		userPassword = userPassword.trim();
		
		if (userName.length() >= 6 && userName.length() <= 14 
				&& userPassword.length()>=6 && userPassword.length()<=14) {
			if (userName.equalsIgnoreCase("fDF fds") && userPassword.equals("A1131 45664")) {
				System.out.println("匹配成功");
				return;
			}else {
				System.out.println("匹配不成功");
				return;
			}
		}
		System.out.println("账号密码不符合规则");			
	}
}
  • 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

周末03

简答题01

1.java环境安装了什么?

2.java环境变量配置了什么?值是什么?

3.数据类型有哪些?

byte short int long float double boolean char

4.给8个基本数据类型赋相应对的值

5.分支语句的语法(if与switch都写)

if(条件语句){
	
}else if(条件语句){

}else{

}

switch(变量){
		case 常量1:
		当变量值等于常量1时,执行此处代码
		break;
		case 常量2:
		当变量值等于常量2时,执行此处代码
		break;
		...
		case 常量n:
		当变量值等于常量n时,执行此处代码
		break;
		default:
		当变量值不等于以上常量时执行此处代码
		break;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

6.循环语句的语法(while,do while,for)

while(){}   do{}while()  for(i=0,i<args.lenght ,i++){}
  • 1

7.方法定义的语法与使用的语法?

    访问权限修饰符 修饰符 返回值类型 方法名(形参列表){}   调用:方法名(形参列表);(有返回值方法可以接收)
  • 1

8.一维数组定义的语法?

数据类型[] 数组名 = new 数据类型[]{};(方括号内可以写数组长度,大括号内可以写直接写数组值,数据长度就等于后面的数组值个数)

静态创建:数据类型[] 数组名 = {};
  • 1
  • 2
  • 3

9.获取数组长度? 10.获取数组中对应下标的值? 11.修改数组对应下标的值?

数组名.length;

数组名[下标];  

数组名[下标] = 值;
  • 1
  • 2
  • 3
  • 4
  • 5

12.类的定义? 13.对象如何创建?如何调用属性,方法?

多个对象抽取其共同点形成的概念
类名() 对象名 = new 类名(); 
对象名.属性;
对象名.方法();
  • 1
  • 2
  • 3
  • 4

14.访问权限修饰符有哪些?他们的意义是什么?

关键字				作用					  中文名
public			当前项目中都可用		   公共的
protected		同个包中或继承关系中可用	受保护的
默认的			  同个包可用				   默认的
private 		当前类中可用				私有的

可以修饰:属性,方法,构造函数,类(内部类)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

15.A类继承与B类的语法格式?

访问权限修饰符 class A extends B{}
  • 1

16.继承的特点有哪些?

子类只能有一个直接父类
子类可以继承所有父类的属性和方法;(在访问权限修饰符允许的前提下)
  • 1
  • 2

17.子类对象转父类对象,父类对象转子类对象

父类名() 对象名 = 子类对象;(自动转换)
子类名() 对象名 = (子类名)父类对象;
  • 1
  • 2

18.抽象类的特点?19.抽象类与普通类的区别?

抽象类被继承时子类必须是抽象类或重写父类的抽象方法
abstract与final不能同时使用
抽象类中不一定有抽象方法
抽象类不能直接创建对象
  • 1
  • 2
  • 3
  • 4

20.static可以修饰哪些东西?有什么作用?

			静态属性属于该类的所有对象
			非静态属性属于该类的对象
			静态属性可以使用类名或对象名调用
			非静态属性只能使用对象名调用
			静态属性在类加载时就被初始化了
			非静态属性在对象创建时被初始化
			
静态方法可以被继承,但不能被重写
静态代码块只在类加载时被执行一次
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

21.final可以修饰哪些东西,有什么作用?

1.final修饰的变量,值不被修改
2.final修饰的局部变量只能赋值一次
3.final修饰的属性必须赋值
4.final修饰的变量名要求全大写(潜规则)
final修饰的方法不能被重写
final修饰的类不能被继承
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

简答题02

1,字符串常用方法

equals();equalsIgnoreCase();
startsWith:判断字符串以什么开始
trim:忽略前后空白
contains:判断字符串是否在当前字符串中存在
indexOf:判断子字符串在字符串中第一次出现的位置,如果不存在返回-1
lastIndexOf:判断子字符串在字符串中最后一次出现的位置,如果不存在返回-1
length:获取字符串长度
toCharArray:将字符串转化为字符(char)数组
getBytes:将字符串转化为字节(Byte)数组
new String(byte[] bs),new String(char[] cs),new String(bytes, charset):将字节数组转换为特定编码格式的字符串(charset:编码格式)
charAt(int index):获取指定位置字符
replace(oldStr, newStr):替换
substring(开始位置):截取,从开始位置到字符串末尾
substring(开始位置,结束位置):截取,从开始位置到结束位置-1
split(字符串):切割
toUpperCase:将小写转换为大写
toLowerCase:将大写转换为小写
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2,可变字符串与不可变字符串的区别

存储位置不同,不可变字符串会在常量区多次占据内存,大量运算时导致程序卡顿,甚至崩溃
可变字符串:字符串缓冲区
  • 1
  • 2

3,StringBuffer与StringBuilder的区别

buffer线程安全   builder线程不安全
  • 1

4,可变字符串常用方法

append:给尾部添加
insert:参数1:插入位置,参数2:插入的字符串
delete:删除
replace:替换一段字符串(开始,结束,替换后的)
toString:转换为字符串
  • 1
  • 2
  • 3
  • 4
  • 5

5,BigDecimal如何创建,与常用方法

BigDecimal 对象名 = new BigDecimal(参数);
	注意:创建时参数用String型,其他数据类型会丢失精度
  • 1
  • 2

6,接口创建的语法

public class interface 接口名{} 
  • 1

7,接口使用接口,类使用接口的语法

public class interface 子接口名 extends 父接口1,父接口2,...{} 
public class interface 子类名 implements 父接口1,父接口2,...{} 
  • 1
  • 2

8,匿名内部类创建的语法格式

父类名/接口名 对象名 =new 父类名/接口名(){
	属性
	方法体
		重写方法
		特有方法
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

9,创建类的语法格式

public class 类名{}
  • 1

10,类中有什么?在哪定义?定义的语法格式

11,如何创建该类的对象?创建对象的时的步骤?

12,对象如何给属性赋值?如何获取属性值?

13,对象如何调用方法?

14,代码块与静态代码块的区别

15,static,final,abstract可以修饰什么?有什么作用

16,静态方法与非静态方法的区别

17,接口与抽象类的区别

18,什么是重写?

19,什么是重载?

20,子类继承父类可以继承什么?

21,子类对象转换为父类对象的语法?

22,父类对象转换为子类对象的语法?

23,子类重写父类方法,然后创建子类对象,在将其转换为父类对象.使用父类对象调用重写的方法.

请问调用的是父类中的方法,还是子类中重写的方法.

24,instanceof关键字的作用与使用的语法格式?

25,如何创建一个数组?

26,如何给数组赋值,取值,查询数组长度

27,循环语句有哪些?语法格式是什么?

28,for循环执行流程

29,while与do while的区别

30,分支语句有哪些?语法格式是什么?

31,break,continue,return分别表示什么意思?

32,java中数据类型有哪些?

编程题

1,创建Dog类提供属性name,sex,age

2,创建Dog对象

来福,雄,3

旺财,雌,2

3,输出来福与旺财的信息(重写toString方法)

4,比较来福与旺财是否是同一个对象(重写equals方法)

public class Test {
	public static void main(String[] args) {
		Dog dog = new Dog("来福","雄",3);
		Dog dog2 = new Dog("旺财","雌",2);
		System.out.println(dog.equals(dog2));
	}
}
public class Dog {
	private String name;
	private String sex;
	private int age;
	public Dog() {
		super();
	}
	public Dog(String name, String sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Dog [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	@Override
		public boolean equals(Object obj) {
		if(obj instanceof Dog) {
			Dog dogs = (Dog)obj;
			if (this.toString().equals(dogs.toString())) {
				return true;
			}
		}
		return false;
	}
}
  • 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

5,获取冒泡排序执行时间的毫秒值

public class MaoDate {
	
	public static void main(String[] args) {
		long start = System.currentTimeMillis();//获取当前时间毫秒值
		int[] arr = new int[10009];
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length-1; j++) {
				if(arr[j]>arr[j+1]) {
					arr[j]^=arr[j+1];
					arr[j+1]^=arr[j];
					arr[j]^= arr[j+1];
				}
			}
		}
		long end = System.currentTimeMillis();
		System.out.println(end-start);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

6,使用for循环遍历数组,数组中存在2000个Dog对象,获取代码执行时间

使用foreach遍历数组,数组中存在2000个Dog对象,获取代码执行时间

public class DogDate {
	public static void main(String[] args) {
		Dog[] dogs = new Dog[2000];
        
        //for循环
		long start = System.currentTimeMillis();//获取当前时间毫秒值
		for (int i = 0; i < dogs.length; i++) {
			System.out.println(dogs.toString());
		}
		long end = System.currentTimeMillis();
		System.out.println(end-start);
		
		//foreach
		long start1 = System.currentTimeMillis();//获取当前时间毫秒值
		for (Dog dog : dogs) {
			System.out.println(dogs.toString());
		}
		long end1 = System.currentTimeMillis();
		System.out.println(end1-start1);
	}
}

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

8,创建Cat类,

​ 并创建

​ 蓝猫,雄,3

使用蓝猫对象与来福对象对比

public class Test {
	public static void main(String[] args) {
		Dog dog = new Dog("来福","雄",3);
		Dog dog2 = new Dog("旺财","雌",2);
		System.out.println(dog.equals(dog2));
		
		Cat cat = new Cat("蓝猫","雌",3);
		System.out.println(dog.equals(cat));
	}
}
public class Cat {
	private String name;
	private String sex;
	private int age;
	public Cat() {
		super();
	}
	public Cat(String name, String sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Cat [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	
}
  • 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

9,使用匿名内部类实现按钮点击

public class Test02{
	public static void main(String[] args) {
		Button button = new Button();
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void click() {
				// TODO Auto-generated method stub
				System.out.println("欢迎光临");
			}
		};//匿名内部类实现接口回调
		
		button.setListener(onClickListener);
		System.out.println("//");
		button.down();
	}
}
public class Button {
	private OnClickListener listener;

	public void setListener(OnClickListener listener) {
		this.listener = listener;
	}
	
	public void down() {
		listener.click();
	}	
}
public interface OnClickListener {
	void click();
}
  • 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

10,使用接口完 成计算机与USB案例

package Day13;
public class Computer {

	private USBInterface usb01;
	private USBInterface usb02;
	private USBInterface usb03;
	private boolean isOpen = false;
	public Computer() {
		super();
	}
	
	public void setUsb01(USBInterface usb01) {
		if(this.usb01 != null && this.isOpen) {
			this.usb01.close();
		}
		
		this.usb01 = usb01;
		if(this.isOpen) {
			this.usb01.conn();
		}
	}
	public void setUsb02(USBInterface usb02) {
		if(this.usb02 != null && this.isOpen) {
			this.usb02.close();
		}
		
		this.usb02 = usb02;
		if(this.isOpen) {
			this.usb02.conn();
		}
	}
	public void setUsb03(USBInterface usb03) {
		if(this.usb03 != null && this.isOpen) {
			this.usb03.close();
		}
		
		this.usb03 = usb03;
		if(this.isOpen) {
			this.usb03.conn();
		}
	}
	
	public void open() {
		System.out.println("准备开机中");
		if (this.usb01 != null) {
			this.usb01.conn();
		}

		if (this.usb02 != null) {
			this.usb02.conn();
		}

		if (this.usb03 != null) {
			this.usb03.conn();
		}

		this.isOpen = true;
		System.out.println("开机成功");
	}
	public void off() {
        System.out.println("准备关机中");
        if (this.usb01 != null) {
            this.usb01.close();
        }

        if (this.usb02 != null) {
            this.usb02.close();
        }

        if (this.usb03 != null) {
            this.usb03.close();
        }

        this.isOpen = false;
        System.out.println("已经关机");
    }
}


//usb接口
package Day13;
public interface USBInterface {
	void conn();
	void close();
}


//扩展器
package Day13;
public class USBKuo implements USBInterface{
	private USBInterface usb01;
	private USBInterface usb02;
	private USBInterface usb03;
	private boolean isOpen = false;
	
	public void setUsb01(USBInterface usb01) {
		if(this.usb01 != null && this.isOpen) {
			this.usb01.close();
		}
		
		this.usb01 = usb01;
		if(this.isOpen) {
			this.usb01.conn();
		}
	}
	public void setUsb02(USBInterface usb02) {
		if(this.usb02 != null && this.isOpen) {
			this.usb02.close();
		}
		
		this.usb02 = usb02;
		if(this.isOpen) {
			this.usb02.conn();
		}
	}
	public void setUsb03(USBInterface usb03) {
		if(this.usb03 != null && this.isOpen) {
			this.usb03.close();
		}
		
		this.usb03 = usb03;
		if(this.isOpen) {
			this.usb03.conn();
		}
	}
	
	@Override
	public void conn() {
		
		if (this.usb01 != null) {
			this.usb01.conn();
		}

		if (this.usb02 != null) {
			this.usb02.conn();
		}

		if (this.usb03 != null) {
			this.usb03.conn();
		}

		this.isOpen = true;
		System.out.println("扩展器已连接");
	}

	@Override
	public void close() {
		if (this.usb01 != null) {
            this.usb01.close();
        }

        if (this.usb02 != null) {
            this.usb02.close();
        }

        if (this.usb03 != null) {
            this.usb03.close();
        }

        this.isOpen = false;
		System.out.println("扩展器断开连接");		
	}
}

package Day13;
public class Mouse implements USBInterface{

	@Override
	public void conn() {
		System.out.println("鼠标已经连接");	
	}

	@Override
	public void close() {
		System.out.println("鼠标断开连接");
	}	 
}

package Day13;
public class Key implements USBInterface{

	@Override
	public void conn() {
		System.out.println("键盘已连接");
		
	}

	@Override
	public void close() {
		System.out.println("键盘断开连接");		
	}
}

  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193

Day17

彩票系统(2)

/*思路:
	1,一组中奖号码
	2,一组用户购买的号码
	3,比较两个数组判断中了几等奖*/
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class test {
	public static void main(String[] args) {
		CaiPiao();
	}
	public static void CaiPiao() {
		System.out.println("欢迎来买彩票,祝你好运!!!");
		ArrayList<Integer> yh = YongHu();
		ArrayList<Integer> zd = ZiDong();
		System.out.println(zd);
		int tag = 0;
		for (Integer i : yh) {
			if (zd.contains(i)) {
				tag++;
			}
		}
		switch (tag) {
		case 7:
			System.out.println("恭喜你中了一等奖!!!\n奖金20万");
			break;
		case 6:
			System.out.println("恭喜你中了二等奖!!!\n奖金2万");
			break;
		case 5:
			System.out.println("恭喜你中了三等奖!!!\n奖金5000元");
			break;
		case 4:
			System.out.println("恭喜你中了四等奖!!!\n奖金500元");
			break;
		case 3:
			System.out.println("恭喜你中了五等奖!!!\n奖金5元");
			break;
		default:
			System.out.println("没有中奖,谢谢你为中国彩票做出的贡献");
			break;
		}
	}

	// 自动生成中奖号码
	public static ArrayList<Integer> ZiDong() {
		ArrayList<Integer> list = new ArrayList<Integer>();
		while (list.size() < 7) {
			Random random1 = new Random();
			int tag = random1.nextInt(31) + 1;
			if (!list.contains(tag)) {
				list.add(tag);
			}
		}
		return list;
	}

	// 用户购买彩票号码
	public static ArrayList<Integer> YongHu() {
		Scanner scanner = new Scanner(System.in);
		int tag = 1;
		ArrayList<Integer> list = new ArrayList<Integer>();
		while (list.size() < 7) {
			System.out.println("请输入你第" + tag + "个号码");
			int num = scanner.nextInt();
			if (list.contains(num)) {
				System.out.println("该号码已经存在,请重新输入");
			} else if (num > 31 || num < 1) {
				System.out.println("该号码不在选择范围,请重新输入");
			} else {
				list.add(num);
				tag++;
			}
		}
		return list;
	}
}
  • 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

第二题

//给学校做一个系统.要求存储学员信息,如果学员的所有信息都相同,认为是同一个学员.将其剔重
public class Demo02 {
	public static void main(String[] args) {
		Student  s01 = new Student("w", "f", "q", 28);
		Student  s02 = new Student("d", "d", "d", 28);
		Student  s03 = new Student("dd", "d", "v", 28);
		Student  s04 = new Student("q", "s", "d", 28);
		Student  s05 = new Student("ds", "d", "d", 28);
		Student  s06 = new Student("d", "d", "d", 28);
		HashSet<Student> set = new HashSet<Student>();//Set可以查重(TreeSet自己用比较器实现)
		set.add(s01);
		set.add(s02);
		set.add(s03);
		set.add(s04);
		set.add(s05);
		set.add(s06);
		System.out.println(set);
	}
}
public class Student {
	private String name;
	private String sex;
	private String id;
	private int age;
	public Student() {
		super();
	}
	public Student(String name, String sex, String id, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.id = id;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", id=" + id + ", age=" + age + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}	
}

  • 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
  • 101
  • 102

银行业务(Plus)

在原来银行业务的基础上增加彩票模块

import java.util.Scanner;

public class TestBank {
	public static void main(String[] args) {
		Bank bank = new Bank("张氏银行");
		while (true) {
			System.out.println(bank.getName() + "欢迎您");
			one(bank);
		}
		
	}
	
	//一级菜单
	@SuppressWarnings("resource")
	public static void one(Bank bank) {
		System.out.println("欢迎你,可选择的操作有:\n1.注册\n2.登录");
		System.out.println("请输入你需要的选择编码");
		Scanner scanner = new Scanner(System.in);
		String tag = scanner.next();//没有用int类型接收,而是用String提供的hashCode获得1.2的哈希码值
		switch (tag.hashCode()) {
		case 49:
			if (tag.equals("1")) {
				bank.regist();
				return;
			}
			break;
		case 50:
			if (tag.equals("2")) {
				User user = bank.login();
				if(user != null) {
					two(bank, user);
					}
				return;
			}
			break;
		}
		System.out.println("输入有误,请重新输入");
	}
	
	//	死循环包裹 存钱,取钱,查询余额,退出	
	//二级菜单
	public static void two(Bank bank, User user) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("欢迎你,可选择的操作有:\n1.存钱\n2.取钱\n3.查询余额\n4.买彩票\n5.退出操作");
			System.out.println("请输入你需要的选择编码");
			int tag = scanner.nextInt();
			if (tag == 1) {
				bank.saveMoney(user);
			}else if (tag == 2) {
				bank.getMoney(user);
			}else if (tag == 3) {
				bank.lookMoney(user);
			}else if(tag == 4){
				bank.CaiPiao(user);
			}else if (tag == 5) {
				return;
			}else {
				System.out.println("选择有误请重新输入");
			}
			
		}
	}
}

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

public class Bank {
	private String name;
	private static ArrayList<User> users = new ArrayList<User>();
	
	public Bank() {
		super();
	}
	public Bank(String name) {
		super();
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	//注册
	public boolean regist() {
		System.out.println("欢迎来到张氏银行!!!");
		System.out.println("请输入你的昵称");
		Scanner scanner = new Scanner(System.in);
		String name = scanner.next();
		System.out.println("请输入你的账号");
		String userName = scanner.next();
		System.out.println("请输入输入你的密码");
		String password01 = scanner.next();
		System.out.println("请再一次输入你的密码");
		String password02 = scanner.next();
		
		boolean isSave = isSave(userName);
		if (isSave) {
			System.out.println("该用户已经存在请重新输入");
			return false;
		}
		if (!password01.equals(password02)) {
			System.out.println("两次输入密码不一致,请重新创建");
			return false;
		}
		User user = new User(name, userName, password01, 0);
		users.add(user);
		System.out.println("恭喜您!!!用户创建success");
		return true;	
	}
	//登录
	public User login() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入你的账号");
		String username = scanner.next();
		System.out.println("请输入你的密码");
		String password = scanner.next();
		String i = Suiji();
		System.out.println(i);
		System.out.println("请输入上述随机验证码(不区分大小写)");
		String m = scanner.next();
		if (users != null) {
			for (User user : users) {
				if (user != null && 
						user.getUserName().equals(username) && 
						user.getPassword().equals(password) && 
						i.equalsIgnoreCase(m)) {
					System.out.println("登录成功");
					return user;
				}
			}
		}
		System.out.println("登录失败");
		return null;
	}
	
	/**
	 * 判断该用户是否已经创建
	 * @param username
	 * @return true为已经存在
	 */
	public boolean isSave(String username) {
		if (users != null ) {
			//集合遍历会产生空指针异常
			for (User user :  users) {
				//刚开始把判断放前面,user可能为空调用getUserName()会产生空指针异常
				if (user != null && username.equals(user.getUserName()) ) {
					return true ;
				}
			}
		}
		return false;
	}
	//生成四位随机数
	public String Suiji() {
		String i="";
		Random random = new Random();
		for (int j = 0; j < 4; j++) {
			int s = 0;
			int m = random.nextInt(3);
			switch (m) {
			case 0:
				 s = random.nextInt(10);
				 i += s + "";
				break;
			case 1:
				 s = random.nextInt(25)+65;
				 i += (char)s;
				break;
			case 2:
				 s = random.nextInt(25)+97;
				 i += (char)s;
				break;
			default:
				break;
			}	
		}
		return i;
	}
	
	//存钱
	public void saveMoney(User user) {
		System.out.println("请输入你本次的存款金额");
		Scanner scanner = new Scanner(System.in);
		int money = scanner.nextInt();
		if (money > 0) {
			int oldMoney = user.getMoney();
			int newMoney = oldMoney + money;
			user.setMoney(newMoney);
			System.out.println("原余额为:"+oldMoney+"元\n本次存入:"+money+"元\n当前余额为:"+newMoney);
		} else {
			System.out.println("存入金额有误");
		}
	}
	
	//取钱
	public void getMoney(User user) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入您本次的取款金额");
		int money = scanner.nextInt();
		int oldMoney = user.getMoney();
		if (money>0 && money < oldMoney) {
			int newMoney = oldMoney - money;
			user.setMoney(newMoney);
			System.out.println("原余额为:"+oldMoney+"元\n本次取款:"+money+"元\n当前余额为:"+newMoney);
		}else if (money>0 && money > oldMoney) {
			System.out.println("当前余额不足");
		}else {
			System.out.println("取款金额有误");
		}
	}
	
	//查询余额
	public void lookMoney(User user) {
		int money = user.getMoney();
		System.out.println("当前账户余额为:"+money+"元");
	}
	
	//彩票功能
	public void CaiPiao(User user) {
		if (user.getMoney()>2) {
			System.out.println("欢迎来买彩票,祝你好运!!!");
			int money = user.getMoney()-2;//每次进入扣费两元
			
			ArrayList<Integer> yh = YongHu();
			ArrayList<Integer> zd= ZiDong();
			System.out.println(zd);
			int tag = 0;
			for (Integer i : yh) {
				if(zd.contains(i)) {
					tag++;
				}
			}
			switch (tag) {
			case 7:
				System.out.println("恭喜你中了一等奖!!!\n奖金20万奖金已存入你的银行账户");
				money += 200000;
				break;
			case 6:
				System.out.println("恭喜你中了二等奖!!!\n奖金2万奖金已存入你的银行账户");
				money += 20000;
				break;
			case 5:
				System.out.println("恭喜你中了三等奖!!!\n奖金5000元奖金已存入你的银行账户");
				money += 5000;
				break;
			case 4:
				System.out.println("恭喜你中了四等奖!!!\n奖金500元奖金已存入你的银行账户");
				money += 500;
				break;
			case 3:
				System.out.println("恭喜你中了五等奖!!!\n奖金5元奖金已存入你的银行账户");
				money += 5;
				break;
			default:
				System.out.println("没有中奖,谢谢你为中国彩票做出的贡献");
				break;
			}
			user.setMoney(money);
			
		}else {
			System.out.println("没钱就去好好赚钱吧,不要买彩票了");
		}	
	}
	//自动生成中奖号码
	public ArrayList<Integer> ZiDong() {
		ArrayList<Integer> list = new ArrayList<Integer>();
		while (list.size() < 7) {
			Random random1 = new Random();
			int tag = random1.nextInt(31)+1;
			if (!list.contains(tag)) {
				list.add(tag);
			}
		}
		return list;
	}
	//用户购买彩票号码
	public ArrayList<Integer> YongHu() {
		Scanner scanner = new Scanner(System.in);
		int tag = 1 ;
		ArrayList<Integer> list = new ArrayList<Integer>();
		while (list.size() < 7) {
			System.out.println("请输入你第"+tag+"个号码");
			int num = scanner.nextInt();
			if (list.contains(num)) {
				System.out.println("该号码已经存在,请重新输入");
			}else if(num>31 || num<1) {
				System.out.println("该号码不在选择范围,请重新输入");
			}else {
				list.add(num);
				tag++;
			}
		}
		return list;
	}	
}
public class User {
	private String name;
	private String userName;
	private String password;
	private int money;
	public User() {
		super();
	}
	public User(String name, String userName, String password, int money) {
		super();
		this.name = name;
		this.userName = userName;
		this.password = password;
		this.money = money;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}	
}
  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339

Day18

第一题

2,将集合中的数据剔重,该集合为ArrayList,存储的数据有:1,2,3,4,5,6,7,8,9,0,10,1,2,7,11,14,19

public static void chaChong() {
		int[] i = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 1, 2, 7, 11, 14, 19 };
		List<Integer> list = new ArrayList<Integer>();
		List<Integer> list2 = new ArrayList<Integer>();
		for (int m : i) {
			list.add(m);
		}
		System.out.println(list);
		//方法一
//		List<Integer> listWithoutDuplicates = list.stream().distinct().collect(Collectors.toList());
//		System.out.println(listWithoutDuplicates);
		
		//方法二
		LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(list);
		ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
		System.out.println(listWithoutDuplicates);
		
		//方法三
		for (Integer integer : list) {
			if (!list2.contains(integer)) {
				list2.add(integer);
			}
		}
		System.out.println(list2);
	}
  • 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

第二题(1)

3,获取两个集合的交集
题目1:HashSet
集合1数据:1,2,3,4,5,6,7
集合2数据:4,5,6,7,8,9
题目2:ArrayList
集合1数据:1,2,3,4,5,6,7
集合2数据:4,5,6,7,8,9

public static void jiaoJi() {
		int[] i = {1,2,3,4,5,6,7};
		int[] m = {4,5,6,7,8,9}; 
		HashSet<Integer> set01 = new HashSet<Integer>();
		for (int i1 : i) {
			set01.add(i1);
		}
		HashSet<Integer> set02 = new HashSet<Integer>();
		for (int m1 : m) {
			set02.add(m1);
		}
		List<Integer> list01 = new ArrayList<Integer>();
		for (int i1 : i) {
			list01.add(i1);
		}
		List<Integer> list02 = new ArrayList<Integer>();
		List<Integer> list03 = new ArrayList<Integer>();
		for (int m1 : m) {
			list02.add(m1);
		}
		for (Integer integer : list02) {
			if (list01.contains(integer)) {
				list03.add(integer);
			}
		}
		System.out.println(list03);
//		//求集合1与集合2的交集
//		list01.retainAll(list02);
//		System.out.println(list01);
//		
//		set01.retainAll(set02);
//		System.out.println(set01);	
		
		
	}
  • 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

第二题(2)

题目3:HashMap(交集)
​ map1的数据:1=java,2=python,3=html
​ map2的数据:2=python,3=html,4=css,5=javascript

public static void mapJiaoJi() {
		HashMap<Integer, String> map1 = new HashMap<Integer, String>();
		map1.put(1, "java");
		map1.put(2, "python");
		map1.put(3, "html");
		HashMap<Integer, String> map2 = new HashMap<Integer, String>();
		HashMap<Integer, String> map3 = new HashMap<Integer, String>();
		map2.put(2, "python");
		map2.put(3, "html");
		map2.put(4, "css");
		map2.put(5, "javascript");
		//Set<Integer> keySet = map1.keySet();
		Set<Entry<Integer, String>> entrySet = map1.entrySet();
		for (Entry<Integer, String> entry : entrySet) {
			if(map2.containsKey(entry.getKey()) && map2.containsValue(entry.getValue())) {
				map3.put(entry.getKey(), entry.getValue());
			}
		}
		System.out.println(map3);
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

第三题(1)

4,获取两个集合的并集
题目1:HashSet
集合1数据:1,2,3,4,5,6,7
集合2数据:4,5,6,7,8,9
题目2:ArrayList
集合1数据:1,2,3,4,5,6,7
集合2数据:4,5,6,7,8,9

public static void bingJi() {
		int[] i = {1,2,3,4,5,6,7};
		int[] m = {4,5,6,7,8,9}; 
		HashSet<Integer> set01 = new HashSet<Integer>();
		for (int i1 : i) {
			set01.add(i1);
		}
		HashSet<Integer> set02 = new HashSet<Integer>();
		for (int m1 : m) {
			set02.add(m1);
		}
		HashSet<Integer> set03 = new HashSet<Integer>();
		List<Integer> list01 = new ArrayList<Integer>();
		for (int i1 : i) {
			list01.add(i1);
		}
		List<Integer> list02 = new ArrayList<Integer>();
		List<Integer> list03 = new ArrayList<Integer>();
		for (int m1 : m) {
			list02.add(m1);
		}
		for (Integer integer : list02) {
			if (!list01.contains(integer)) {
				list03.add(integer);
			}
		}
		list03.addAll(list01);
		System.out.println(list03);
		
		for (Integer s: set02) {
			if (!set01.contains(s)) {
				set03.add(s);
			}
		}
		set03.addAll(set01);
		System.out.println(set03);
//		//求集合1与集合2的并集(先做差集再做添加)
		list01.removeAll(list02);
		list01.addAll(list02);
		System.out.println(list01);
		//set集合不会重复添加自动去重
		set01.addAll(set02);
		System.out.println(set01);			
	}
}
  • 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

第三题(2)

​ 题目3:HashMap(并集)
​ map1的数据:1=java,2=python,3=html
​ map2的数据:2=python,3=html,4=css,5=javascript

public static void mapBingJi() {
		HashMap<Integer, String> map1 = new HashMap<Integer, String>();
		map1.put(1, "java");
		map1.put(2, "python");
		map1.put(3, "html");
		HashMap<Integer, String> map2 = new HashMap<Integer, String>();
		HashMap<Integer, String> map3 = new HashMap<Integer, String>();
		map2.put(2, "python");
		map2.put(3, "html");
		map2.put(4, "css");
		map2.put(5, "javascript");
		Set<Entry<Integer, String>> entrySet = map1.entrySet();
		for (Entry<Integer, String> entry : entrySet) {
			if(!map2.containsKey(entry.getKey())) {
				map3.put(entry.getKey(), entry.getValue());//把map1中map2没有的元素添加一遍
			}
		}
		map3.putAll(map2);
		System.out.println(map3);
	}
	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

第四题

5,分别使用线程的四种创建方式创建线程1次

public class Demo02 {
	public static void main(String[] args) {
		//方法一
		myThread myThread = new myThread();
		//方法二
		Thread thread = new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
			}
		};
		//方法三
		Thread thread2 = new Thread(new myRunnable());
		//方法四
		Thread thread3 = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
			}
		});		
	}
	
	public static void s1() {
		
	}
}

class myThread extends Thread{
	@Override
	public void run() {		
		super.run();
	}
}
class myRunnable implements Runnable {
	public void run() {		
	}
}
  • 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

6,开启三个线程一个打印126,一个打印AZ,一个打印a~z

public class Demo03 {
	public static void main(String[] args) {
		Thread thread01 = new Thread(new Runnable() {			
			@Override
			public void run() {
				for (int i = 1; i < 27; i++) {
					System.out.println(i);
				}
			}
		});
		Thread thread02 = new Thread(new Runnable() {			
			@Override
			public void run() {
				for (int i = 65; i < 91; i++) {
					char m = (char)i;
					System.out.println(m);
				}
			}
		});
		Thread thread03 = new Thread(new Runnable() {			
			@Override
			public void run() {
				for (int i = 97; i < 123; i++) {
					char m = (char)i;
					System.out.println(m);
				}
			}
		});
		thread01.start();
		thread02.start();
		thread03.start();
	}
}

  • 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

7,模拟窗口售票案例
要求:四个窗口同时销售1000张票,观察结果

public class Demo2 {
	public static void main(String[] args) {
		myRunnables myRunnable = new myRunnables();
		Thread thread1 = new Thread(myRunnable,"窗口1");
		Thread thread2 = new Thread(myRunnable,"窗口2");
		Thread thread3 = new Thread(myRunnable,"窗口3");
		Thread thread4 = new Thread(myRunnable,"窗口4");
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}

class myRunnables implements Runnable {
	private int num = 1000;
	@Override
	public void run() {
		while (num > 0) {
			num--;
			String name = Thread.currentThread().getName();
			System.out.println(name + "卖出一张票,还剩" + num + "张票");
		}
	}
}
  • 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

附加题:
使用两个线程,一个打印152,一个打印AZ
打印结果要求为:A12B34C56D78E910…1,使用sleep();实现

public class Demo04 {
	public static void main(String[] args) {
		Thread thread01 = new Thread(new Runnable() {			
			@Override
			public void run() {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				for (int i = 1; i < 53; i+=2) {
					System.out.println(i);
					System.out.println(i+1);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		Thread thread02 = new Thread(new Runnable() {			
			@Override
			public void run() {
				
				for (int i = 65; i < 91; i++) {
					char m = (char)i;
					System.out.println(m);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}			
			}
		});
		
		thread01.start();
		thread02.start();
	}
}
  • 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

Day19

第一题

2,四个窗口销售1000张票,线程安全

public class Demo01 {
	public static void main(String[] args) {
		myRunnable myRunnable = new myRunnable();
		Thread thread1 = new Thread(myRunnable,"窗口1");
		Thread thread2 = new Thread(myRunnable,"窗口2");
		Thread thread3 = new Thread(myRunnable,"窗口3");
		Thread thread4 = new Thread(myRunnable,"窗口4");
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}
class myRunnable implements Runnable{
	private int num = 1000;
	private Object lock = new Object();//直接synchronized (new Object()),每个线程的锁不一样,不会起作用
	@Override
	public void run() {
		while (num > 0) {
			synchronized (lock) {
				if (num<=0) {
					return;
				}
				num--;
				String name = Thread.currentThread().getName();
				System.out.println(name+"卖出一张票,还剩"+num+"张票");	
			}	
		}	
	}
	
}
  • 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
第二题

3,三线程的死锁

public class Demo05 {
	private static Object lock1 = new Object();
	private static Object lock2 = new Object();
	private static Object lock3 = new Object();
	
	public static void main(String[] args) {
		Thread thread01 = new Thread(new Runnable() {			
			@Override
			public void run() {
				synchronized(lock1) {
					System.out.println("suo2");
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					synchronized (lock2) {
						
					}
				}
			}
		});
		Thread thread02 = new Thread(new Runnable() {			
			@Override
			public void run() {
				synchronized(lock2) {
					System.out.println("suo1");
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					synchronized (lock3) {
						
					}
				}
			}
		});
		Thread thread03 = new Thread(new Runnable() {			
			@Override
			public void run() {
				synchronized(lock3) {
					System.out.println("suo3");
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					synchronized (lock1) {
						
					}
				}
			}
		});
		thread01.start();
		thread02.start();
		thread03.start();
	}
}
  • 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
第三题

4,打印A12B34C56…
线程间通讯

public class Demo07 {
	public static void main(String[] args) {
		Object lock = new Object();
		//打印字母
		Thread thread01 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				synchronized (lock) {
					for (int i = 65; i < 91; i++) {
						char c = (char) i;
						System.out.print(c);
						lock.notify();
						try {
							lock.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				
			}
		});
		
		//打印数字
		Thread thread02 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				synchronized (lock) {
					try {
						lock.wait();
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					for (int i = 1; i < 53; i++) {
						if (i%2 == 1) {
							System.out.print(i);
						}else {
							System.out.print(i);
							lock.notify();
							if (i != 52) {
								try {
									lock.wait();
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}	
						}
					}
				}
			}
		});
		thread01.start();
		thread02.start();
	}
}

//方法二
public class Demo06 {
	public static void main(String[] args) {
		Object lock = new Object();
		Thread thread01 = new Thread(new Runnable() {			
			@Override
			public void run() {
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				synchronized (lock) {
					for (int i = 1; i < 53; i+=2) {
						System.out.println(i);
						System.out.println(i+1);
						lock.notify();
						try {
							if (i==51) {//最后一次不返回由于另一个线程已经执行完毕没有notify();会一直等待
								return;
							}
							lock.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		});
		Thread thread02 = new Thread(new Runnable() {			
			@Override
			public void run() {
				for (int i = 65; i < 91; i++) {
					synchronized (lock) {
						char m = (char)i;
						System.out.println(m);
						lock.notify();
						try {
							lock.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		});
		
		thread01.start();
		thread02.start();
	}
}

  • 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
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
第四题

5,龟兔赛跑

public class GuiTuDemo {
	public static void main(String[] args) {
		Object lock = new Object();
		Thread thread01 = new Thread(new Runnable() {	
			public void run() {
				int num = 100;
				while(num > 0) {
					num-=1;
					System.out.println("乌龟还剩"+num+"米");
				}
				synchronized (lock) {
					lock.notify();
				}
			}
		});
		Thread thread02 = new Thread(new Runnable() {
			public void run() {
				int num = 100;
				while(num > 0) {
					synchronized (lock) {
						if (num==80) {
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					num-=5;
					System.out.println("兔子还剩"+num+"米");
				}	
			}
		});
		
		thread01.start();
		thread02.start();
	}
}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/233537
推荐阅读
相关标签
  

闽ICP备14008679号