文字版格斗游戏
格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候(new对象的时候),这些信息就应该被确定下来
package test5;
public class GameTest {
public static void main(String[] args) {
Role r1 = new Role("小明" , 100) ;
Role r2 = new Role("小虎" ,100) ;
while(true){
r1.attack(r2);
if(r2.getBlood() == 0){
System.out.println(r2.getName() + "败北");
break;
}
r2.attack(r1);
if(r1.getBlood() == 0){
System.out.println(r1.getName() + "败北");
break;
}
}
}
}
package test5;
import java.util.Random;
public class Role {
private String name;
private int blood;
public Role() {
}
public Role(String name, int blood) {
this.name = name;
this.blood = blood;
}
/**
* 获取
*
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
*
* @return blood
*/
public int getBlood() {
return blood;
}
/**
* 设置
*
* @param blood
*/
public void setBlood(int blood) {
this.blood = blood;
}
public void attack(Role role) {
//随机造成伤害
Random r = new Random();
int hurt = r.nextInt(20) + 1;
//血量计算
int remainBoold = role.getBlood() - hurt;
remainBoold = remainBoold < 0 ? 0 : remainBoold;
role.setBlood(remainBoold);
System.out.println(this.getName() + "攻击了" + role.getName() + "造成了" + hurt + "点伤害,还剩下" + remainBoold + "点血");
}
}
对象数组
定义数组存储3个商品对象
商品的属性:商品的id,名字,价格,库存
创建三个商品对象,并把商品对象存入到数组当中
package test6;
public class GoodsTest {
public static void main(String[] args) {
Goods[] arr = new Goods[3];
Goods g1 = new Goods("001", "华为", 5999.0, 100);
Goods g2 = new Goods("002", "苹果", 6999.0, 50);
Goods g3 = new Goods("003", "oppo", 4999.0, 70);
arr[0] = g1;
arr[1] = g2;
arr[2] = g3;
for (int i = 0; i < arr.length; i++) {
Goods goods = arr[i];
System.out.println(goods.getId() + " " + goods.getName() + " " + goods.getPrice() + " " + goods.getCount());
}
}
}
package test6;
public class Goods {
private String id;
private String name;
private double price;
private int count;
public Goods() {
}
public Goods(String id, String name, double price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return price
*/
public double getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 获取
* @return count
*/
public int getCount() {
return count;
}
/**
* 设置
* @param count
*/
public void setCount(int count) {
this.count = count;
}
public String toString() {
return "Goods{id = " + id + ", name = " + name + ", price = " + price + ", count = " + count + "}";
}
}
手动输入
package test7;
public class Test {
private String brand;
private int price;
private String color;
public Test() {
}
public Test(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
/**
* 获取
*
* @return brand
*/
public String getBrand() {
return brand;
}
/**
* 设置
*
* @param brand
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* 获取
*
* @return price
*/
public int getPrice() {
return price;
}
/**
* 设置
*
* @param price
*/
public void setPrice(int price) {
this.price = price;
}
/**
* 获取
*
* @return color
*/
public String getColor() {
return color;
}
/**
* 设置
*
* @param color
*/
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "Test{brand = " + brand + ", price = " + price + ", color = " + color + "}";
}
}
package test7;
import java.util.Scanner;
public class CarTest {
public static void main(String[] args) {
Test[] arr = new Test[3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
Test t = new Test();
System.out.println("请输入品牌");
String brand = sc.next();
t.setBrand(brand);
System.out.println("请输入价格");
int price = sc.nextInt();
t.setPrice(price);
System.out.println("请输入颜色");
String color = sc.next();
t.setColor(color);
arr[i] = t;
}
for (int i = 0; i < arr.length; i++) {
Test test = arr[i];
System.out.println(test.getBrand() + " " + test.getPrice() + " " + test.getColor());
}
}
}
学生删除添加
定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。
学生的属性:学号,姓名,年龄
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
要求2:添加完毕之后,遍历所有学生信息
要求3:通过id删除学生信息
要求4:查询学生信息,如果存在,则将他的年龄+1岁
package test8;
public class Student {
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
/**
* 获取
* @return id
*/
public int getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(int id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + "}";
}
}
package test8;
public class StudentTest {
public static void main(String[] args) {
Student[] arr = new Student[3];
Student stu1 = new Student(1, "A", 23);
Student stu2 = new Student(2, "B", 21);
arr[0] = stu1;
arr[1] = stu2;
Student stu4 = new Student(4, "D", 22);
boolean flag = contains(arr, stu4.getId());
if (flag) {
System.out.println("id重复");
} else {
int count = getCount(arr);
if (count == arr.length) {
Student[] newArr = creatNew(arr);
newArr[count] = stu4;
printArr(newArr);
} else {
arr[count] = stu4;
printArr(arr);
}
}
int index = getId(arr, 3);
int did = getId(arr, 4);
if (index >= 0) {
arr[index] = null;
printArr(arr);
} else {
System.out.println("当前id不存在");
}
if (did >= 0) {
Student stu = arr[did];
int newAge = stu.getAge() + 1;
stu.setAge(newAge);
printArr(arr);
} else {
System.out.println("当前id不存在");
}
}
public static int getId(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
Student stu = arr[i];
if (stu != null) {
int sid = stu.getId();
if (sid == id) {
return i;
}
}
}
return -1;
}
public static int getCount(Student[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
count++;
}
}
return count;
}
public static Student[] creatNew(Student[] arr) {
Student[] newArr = new Student[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static boolean contains(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
Student stu = arr[i];
if (stu != null) {
int sid = stu.getId();
if (sid == id) {
return true;
}
}
}
return false;
}
public static void printArr(Student[] arr) {
for (int i = 0; i < arr.length; i++) {
Student stu = arr[i];
if (arr[i] != null) {
System.out.println(stu.getId() + " " + stu.getName() + " " + stu.getAge());
}
}
}
}
Tips
以上学习内容均来自于B站黑马程序员