01数组
练习:双色球
import java.util.Random;
/**
* 双色球的遍历
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class DoubleColorBall {
public static void main(String[] args) {
int[] arr1 = new int[7];
//随机数的生成
Random random = new Random();
for (int i = 0; i < arr1.length - 1; i++) {
//表示1-33 默认表示0-32
arr1[i] = random.nextInt(1,33);
//去重
for (int j = i - 1; j >= 0; j--) {
if (arr1[i] == arr1[j]) {
i--;
break;
}
}
}
arr1[arr1.length - 1] = random.nextInt(1,17);
System.out.println("本期中奖结果:");
for (int i : arr1) {
System.out.print(i + " ");
}
}
}
02面向对象
练习一
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class Girl {
String name;
int age;
boolean bf;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setBf(boolean bf) {
this.bf = bf;
}
public String print(String name, int age, boolean boy) {
return "我叫:" + name + ",我今年" + age + "岁了,我" + boy + "男朋友!";
}
public static void main(String[] args) {
Girl girl = new Girl();
Scanner sc = new Scanner(System.in);
System.out.println("输入姓名:");
girl.setName(sc.next());
System.out.println("输入年龄");
girl.setAge(sc.nextInt());
System.out.println("有男朋友嘛");
girl.setBf(sc.hasNext());
System.out.println(girl.print(girl.name, girl.age, girl.bf));
}
}
练习二
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class Vehicle {
double speed;
String type;
public void move(){
System.out.println("类型:" + type + "的当前车速为" + speed);
}
public void setSpeed(double s) {
this.speed = s;
}
public void speedUp(double s) {
speed = s + speed;
}
public void speedDown(double s) {
this.speed = speed - s;
}
public Vehicle(double speed, String type) {
this.speed = speed;
this.type = type;
}
public static void main(String[] args) {
Vehicle v = new Vehicle(12,"奥迪");
v.move();
v.setSpeed(13);
v.move();
v.speedUp(14);
v.move();
v.speedDown(11);
v.move();
}
}
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class Car {
String brand;
String color;
double price;
public Car() {
}
public Car(String brand, String color, double price) {
this.brand = brand;
this.color = color;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void addPrice(){
price = price + 1000;
}
public void addPrice1(double p) {
price = price + p;
}
public void printAll() {
System.out.println("品牌:" + brand + ",颜色:" + color + ",价格:" + price);
}
public static void main(String[] args) {
Car c = new Car();
c.printAll();
Car c1 = new Car("奥迪","黑色",1234564);
c1.printAll();
c1.addPrice();
c1.addPrice1(456);
c1.printAll();
}
}
练习四
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class Circle {
double r;
public double S() {
return Math.PI * r * r;
}
public double R() {
return 2 * Math.PI * r;
}
public Circle(double r) {
this.r = r;
}
public static void main(String[] args) {
Circle circle = new Circle(3);
System.out.println("圆的面积为:" + circle.S());
System.out.println("圆的周长为:" + circle.R());
}
}
练习五
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class Rectangle {
int length;
int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int getArea() {
return width * length;
}
public int getPer() {
return (width + length) * 2;
}
public void showAll() {
System.out.println("面积为:" + getArea() + ",周长为:" + getPer());
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(4,5);
rectangle.showAll();
}
}
03类的封装
练习一
package com.work;
import java.util.Objects;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class User {
//用户编号
private int id;
//用户姓名
private String name;
//年龄
private int age;
//城市
private String city;
//详细住址
private String address;
//邮箱
private String email;
//电话
private String phone;
//微信
private String weixin;
//qq
private String qq;
//微博
private String weibo;
//性别
private String sex;
//简介
private String description;
public User() {
}
public User(int id, String name, int age, String city, String address, String email, String phone, String weixin, String qq, String weibo, String sex, String description) {
this.id = id;
this.name = name;
this.age = age;
this.city = city;
this.address = address;
this.email = email;
this.phone = phone;
this.weixin = weixin;
this.qq = qq;
this.weibo = weibo;
this.sex = sex;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWeixin() {
return weixin;
}
public void setWeixin(String weixin) {
this.weixin = weixin;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getWeibo() {
return weibo;
}
public void setWeibo(String weibo) {
this.weibo = weibo;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return id == user.id && age == user.age && Objects.equals(name, user.name) && Objects.equals(city, user.city) && Objects.equals(address, user.address) && Objects.equals(email, user.email) && Objects.equals(phone, user.phone) && Objects.equals(weixin, user.weixin) && Objects.equals(qq, user.qq) && Objects.equals(weibo, user.weibo) && Objects.equals(sex, user.sex) && Objects.equals(description, user.description);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, city, address, email, phone, weixin, qq, weibo, sex, description);
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", city='" + city + '\'' +
", address='" + address + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", weixin='" + weixin + '\'' +
", qq='" + qq + '\'' +
", weibo='" + weibo + '\'' +
", sex='" + sex + '\'' +
", description='" + description + '\'' +
'}';
}
}
练习二
package com.work;
import java.util.Objects;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/24
*/
public class Work {
private int id;
private int userId;
private String start;
private String end;
private String company;
private String job;
private String description;
private Work next;
public Work() {
}
public Work(int id, int userId, String start, String end, String company, String job, String description, Work next) {
this.id = id;
this.userId = userId;
this.start = start;
this.end = end;
this.company = company;
this.job = job;
this.description = description;
this.next = next;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Work getNext() {
return next;
}
public void setNext(Work next) {
this.next = next;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Work work = (Work) o;
return id == work.id && userId == work.userId && Objects.equals(start, work.start) && Objects.equals(end, work.end) && Objects.equals(company, work.company) && Objects.equals(job, work.job) && Objects.equals(description, work.description) && Objects.equals(next, work.next);
}
@Override
public int hashCode() {
return Objects.hash(id, userId, start, end, company, job, description, next);
}
@Override
public String toString() {
return "Work{" +
"id=" + id +
", userId=" + userId +
", start='" + start + '\'' +
", end='" + end + '\'' +
", company='" + company + '\'' +
", job='" + job + '\'' +
", description='" + description + '\'' +
", next=" + next +
'}';
}
}