2023-05-15

1.文字格斗游戏

public class Role {

    private String name;

    private int health;

    public Role(String name, int health) {

        this.name = name;

        this.health = health;

    }

    public String getName() {

        return name;

    }

    public int getHealth() {

        return health;

    }

    public void attack(Role another) {

        int damage = new Random().nextInt(20) + 1;

        System.out.println(this.name + "举起拳头打了" + another.name + "一下,造成了" + damage + "点伤害," + another.name + "还剩下" + (another.health - damage) + "点血。");

        another.health = Math.max(0, another.health - damage);

        if (another.health == 0) {

            System.out.println(this.name + "K.0.了" + another.name);

        }

    }

}

public class FightingGame {

    public static void main(String[] args) {

        Role qiaofeng = new Role("乔峰", 100);

        Role jiumozhi = new Role("鸠摩智", 100);

        while (qiaofeng.getHealth() > 0 && jiumozhi.getHealth() > 0) {

            qiaofeng.attack(jiumozhi);

            jiumozhi.attack(qiaofeng);

        }

    }

}

2.数组练习

(1)a.

int[] arr = new int[100];

for (int i = 0; i < arr.length; i++) {

    arr[i] = i + 1;

}

int sum = 0;

for (int i = 0; i < arr.length; i += 2) {

    sum += arr[i];

}

System.out.println("奇数位置的和为:" + sum);

(2)冒泡排序

Scanner scanner = new Scanner(System.in);

int[] arr = new int[10];

for (int i = 0; i < arr.length; i++) {

    arr[i] = scanner.nextInt();

}

for (int i = 0; i < arr.length - 1; i++) {

    for (int j = 0; j < arr.length - i - 1; j++) {

        if (arr[j] > arr[j + 1]) {

            int temp = arr[j];

            arr[j] = arr[j + 1];

            arr[j + 1] = temp;

        }

    }

}

System.out.println("排序后的数组为:" + Arrays.toString(arr));

(3)

int[] arr = new int[10];

int sum = 0;

for (int i = 0; i < arr.length; i++) {

    arr[i] = new Random().

3.对象数组练习

(1)

public class Car {

    private String brand;

    private int price;

    private String color;

    public Car(String brand, int price, String color) {

        this.brand = brand;

        this.price = price;

        this.color = color;

    }

    public String getBrand() {

        return brand;

    }

    public int getPrice() {

        return price;

    }

    public String getColor() {

        return color;

    }

}

Scanner scanner = new Scanner(System.in);

Car[] cars = new Car[3];

for (int i = 0; i < cars.length; i++) {

    System.out.println("请输入第" + (i + 1) + "部汽车的品牌、价格、颜色:");

    String brand = scanner.next();

    int price = scanner.nextInt();

    String color = scanner.next();

    cars[i] = new Car(brand, price, color);

}

(2)

public class Phone {

    private String brand;

    private int price;

    private String color;

    public Phone(String brand, int price, String color) {

        this.brand = brand;

        this.price = price;

        this.color = color;

    }

    public String getBrand() {

        return brand;

    }

    public int getPrice() {

        return price;

    }

    public String getColor() {

        return color;

    }

}

Scanner scanner = new Scanner(System.in);

Phone[] phones = new Phone[3];

int sum = 0;

for (int i = 0; i < phones.length; i++) {

    System.out.println("请输入第" + (i + 1) + "部手机的品牌、价格、颜色:");

    String brand = scanner.next();

    int price = scanner.nextInt();

    String color = scanner.next();

    phones[i] = new Phone(brand, price, color);

    sum += price;

}

double avgPrice = (double) sum / phones.length;

System.out.println("三部手机的平均价格为:" + avgPrice);

(3)

public class Girlfriend {

    private String name;

    private int age;

    private String gender;

    private String hobby;

    public Girlfriend(String name, int age, String gender, String hobby) {

        this.name = name;

        this.age = age;

        this.gender = gender;

        this.hobby = hobby;

    }

    public String getName() {

        return name;

    }

    public int getAge() {

        return age;

    }

    public String getGender() {

        return gender;

    }

    public String getHobby() {

        return hobby;

    }

}

Scanner scanner = new Scanner(System.in);

Girlfriend[] girlfriends = new Girlfriend[4];

int sumAge = 0;

for (int i = 0; i < girlfriends.length; i++) {

    System.out.println("请输入第" + (i + 1) + "个女朋友的姓名、年龄、性别、爱好:");

    String name = scanner.next();

    int age = scanner.nextInt();

    String gender = scanner.next();

    String hobby = scanner.next();

    girlfriends[i] = new Girlfriend(name, age, gender, hobby);

    sumAge += age;

}

double avgAge = (double) sumAge / girlfriends.length;

System.out.println("四个女朋友的平均年龄为:" + avgAge);

int count = 0;

for (Girlfriend gf : girlfriends) {

    if (gf.getAge() < avgAge) {

        count++;

        System.out.println(gf.getName() + "、" + gf.getAge() + "、" + gf.getGender() + "、" + gf.getHobby());

    }

}

System.out.println("年龄比平均值低的女朋友有" + count + "个。");

你可能感兴趣的:(2023-05-15)