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 + "个。");

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天是我在乐字节学习的第七天拉,老师讲课很幽默风趣哦,是我喜欢滴类型,每天学习都倍有劲儿~~简直太爱了,今天老师讲...
    嘎嘣脆a阅读 168评论 0 0
  • 01.01_计算机基础知识(计算机概述)(了解) A:什么是计算机?计算机在生活中的应用举例计算机(Compute...
    冰川_阅读 283评论 0 1
  • Java类的定义规则和使用; //[类修饰符] class 类名{ 类的成员 public class Demo ...
    小程的Bald_diary阅读 917评论 0 0
  • 10.01_面向对象(package关键字的概述及作用)(了解) A:为什么要有包将字节码(.class)进行分类...
    冰川_阅读 608评论 0 1
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周书达阅读 756评论 0 0