1. 文字格斗游戏代码:
```java
import java.util.Random;
public class FightingGame {
private String name;
private int health;
public FightingGame(String name, int health) {
this.name = name;
this.health = health;
}
public void attack(FightingGame enemy) {
Random random = new Random();
int damage = random.nextInt(20) + 1;
System.out.printf("%s 举起拳头打了 %s 一下,造成了 %d 点伤害,%s 还剩下 %d 点血。\n",
this.name, enemy.name, damage, enemy.name, Math.max(0, enemy.health - damage));
enemy.health = Math.max(0, enemy.health - damage);
if (enemy.health == 0) {
System.out.println(this.name + " K.O. 了 " + enemy.name);
}
}
public static void main(String[] args) {
FightingGame qiaofeng = new FightingGame("乔峰", 100);
FightingGame jiumozhi = new FightingGame("鸠摩智", 100);
while (qiaofeng.health > 0 && jiumozhi.health > 0) {
qiaofeng.attack(jiumozhi);
if (jiumozhi.health == 0) {
break;
}
jiumozhi.attack(qiaofeng);
}
}
}
```
2. 数组练习代码:
(1)
```java
public class ArrayPractice1 {
public static void main(String[] args) {
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)
```java
import java.util.Scanner;
public class ArrayPractice2 {
public static void main(String[] args) {
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;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
(3)
```java
import java.util.Random;
public class ArrayPractice3 {
public static void main(String[] args) {
int[] arr = new int[10];
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(100) + 1;
}
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
double avg = (double) sum / arr.length;
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < avg) {
count++;
}
}
System.out.println("所有数据的和为:" + sum);
System.out.println("所有数据的平均数为:" + avg);
System.out.println("比平均值小的数据有 " + count + " 个。");
}
}
```
3. 对象数组练习代码:
(1)
```java
import java.util.Scanner;
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 static void main(String[] args) {
Car[] cars = new Car[3];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < cars.length; i++) {
System.out.print("请输入第" + (i+1) + "部汽车的品牌:");
String brand(续)
= scanner.next();
System.out.print("请输入第" + (i+1) + "部汽车的价格:");
int price = scanner.nextInt();
System.out.print("请输入第" + (i+1) + "部汽车的颜色:");
String color = scanner.next();
cars[i] = new Car(brand, price, color);
}
}
}
```
(2)
```java
import java.util.Scanner;
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 static void main(String[] args) {
Phone[] phones = new Phone[3];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < phones.length; i++) {
System.out.print("请输入第" + (i+1) + "部手机的品牌:");
String brand = scanner.next();
System.out.print("请输入第" + (i+1) + "部手机的价格:");
int price = scanner.nextInt();
System.out.print("请输入第" + (i+1) + "部手机的颜色:");
String color = scanner.next();
phones[i] = new Phone(brand, price, color);
}
int sum = 0;
for (int i = 0; i < phones.length; i++) {
sum += phones[i].price;
}
double avg = (double) sum / phones.length;
System.out.println("三部手机的平均价格为:" + avg);
}
}
```
(3)
```java
import java.util.Scanner;
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 static void main(String[] args) {
Girlfriend[] girlfriends = new Girlfriend[4];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < girlfriends.length; i++) {
System.out.print("请输入第" + (i+1) + "个女朋友的姓名:");
String name = scanner.next();
System.out.print("请输入第" + (i+1) + "个女朋友的年龄:");
int age = scanner.nextInt();
System.out.print("请输入第" + (i+1) + "个女朋友的性别:");
String gender = scanner.next();
System.out.print("请输入第" + (i+1) + "个女朋友的爱好:");
String hobby = scanner.next();
girlfriends[i] = new Girlfriend(name, age, gender, hobby);
}
int sum = 0;
for (int i = 0; i < girlfriends.length; i++) {
sum += girlfriends[i].age;
}
double avg = (double) sum / girlfriends.length;
System.out.println("四个女朋友的平均年龄为:" + avg);
int count = 0;
for (int i = 0; i < girlfriends.length; i++) {
if (girlfriends[i].age < avg) {
count++;
System.out.println("比平均年龄小的女朋友信息为:" +
girlfriends[i].name + " " +
girlfriends[i].age + " " +
girlfriends[i].gender + " " +
girlfriends[i].hobby);
}
}
System.out.println("比平均年龄小的女朋友有 " + count + " 个。");
}
}
```