1.文字版格斗游戏
格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候(new对象的时候),这些信息就应该被确定下来,伤害为1~20之间的随机数,当剩余血量为0时,输出K.O语句(名字可以不一样,但是效果必须一致)
答案:
[content_hide]
import java.util.Random;
public class person {
private String name;
private int blood;
public person(){
}
public person(String name , int blood){
this.name = name;
this.blood = blood;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setBlood(int blood){
this.blood = blood;
}
public int getBlood(){
return blood;
}
public void attack(person person){
Random r = new Random();
int hurt = r.nextInt(20) + 1;
int residueBlood = person.getBlood() - hurt;
residueBlood = residueBlood < 0 ? 0 : residueBlood;
person.setBlood(residueBlood);
System.out.println(this.getName() + "举起拳头打了" + person.getName() +
"一下,造成了" + hurt + "点伤害," + person.getName() + "还剩下" + residueBlood + "点血");
}
}
public class IT梦 {
public static void main(String[] args) {
person p1 = new person("迪迦奥特曼",100);
person p2 = new person("哥尔赞",100);
while (true){
p1.attack(p2);
if(p2.getBlood() == 0){
System.out.println(p1.getName() + "K.O了" + p2.getName());
break;
}
p2.attack(p1);
if(p1.getBlood() == 0){
System.out.println(p2.getName() + "K.O了" + p1.getName());
break;
}
}
}
}
[/content_hide]
2.对象数组1
定义数组存储三个商品对象;商品的属性:商品的id,名字,价格,库存创建三个商品对象,并把商品对象存入数组当中
答案:
[content_hide]
public class commodity {
private String id;
private String name;
private double price;
private int count;
public commodity() {
}
public commodity(String id, String name, double price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public class IT梦 {
public static void main(String[] args) {
commodity []arr = new commodity[3];
commodity c1 = new commodity("001" , "电脑" , 7999.0 , 50);
commodity c2 = new commodity("002" , "手机" , 5999.0 , 100);
commodity c3 = new commodity("003" , "键盘" , 199.0 , 1000);
arr[0] = c1;
arr[1] = c2;
arr[2] = c3;
for (int i = 0; i < arr.length; i++) {
commodity c = arr[i];
System.out.println(c.getId() + ", " + c.getName() + ", " + c.getPrice() + ", " + c.getCount());
}
}
}
[/content_hide]
3.对象数组2
定义数组存储3部汽车对象汽车的属性:品牌,价格,颜色创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中
键盘录入拓展:
第一套体系(遇到空格,制表符,回车就停止接收):1. nextInt();——-接收整数2. nextDouble();——-接收小数3. next();——-接收字符串第二套体系(可以接收空格,制表符,遇到回车才停止接收数据):1. nextLine();——-接收字符串注:键盘录入的两套体系不能混用的弊端:先用nextInt,再用nxetLine会导致下面的nextLine接收不到数据
答案:
[content_hide]
public class car {
private String brand;
private double price;
private String color;
public car() {
}
public car(String brand, double price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
import java.util.Scanner;
public class IT梦 {
public static void main(String[] args) {
car[] arr = new car[3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
car c = new car();
System.out.println("请输入第" + (i + 1) + "辆汽车的品牌");
String brand = sc.next();
c.setBrand(brand);
System.out.println("请输入第" + (i + 1) + "辆汽车的价格");
Double price = sc.nextDouble();
c.setPrice(price);
System.out.println("请输入第" + (i + 1) + "辆汽车的颜色");
String color = sc.next();
c.setColor(color);
arr[i] = c;
}
for (int i = 0; i < arr.length; i++) {
car c = arr[i];
System.out.println(c.getBrand() + ", " + c.getPrice() + ", " + c.getColor());
}
}
}
[/content_hide]
4.对象数组3
定义数组存储3部手机对象手机的属性:品牌,价格,颜色要求,计算出三部手机的平均价格
答案:
[content_hide]
public class phone {
private String brand;
private int price;
private String color;
public phone() {
}
public phone(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class IT梦 {
public static void main(String[] args) {
phone[] arr = new phone[3];
phone p1 = new phone("小米",5000,"黑色");
phone p2 = new phone("华为",6000,"白色");
phone p3 = new phone("苹果",10000,"蓝色");
arr[0] = p1;
arr[1] = p2;
arr[2] = p3;
int price = p1.getPrice() + p2.getPrice() + p3.getPrice();
int meanPrice = price / arr.length;
for (int i = 0; i < arr.length; i++) {
phone p = arr[i];
System.out.println(p.getBrand() + ", " + p.getPrice() + ", " + p.getColor());
}
System.out.println("三部手机的平均价格为:" + meanPrice);
}
}
[/content_hide]
5.对象数组4
定义数组存储4个女朋友对象女朋友的属性:姓名,年龄,性别,爱好要求1:计算出4个女朋友的平均年龄要求2:统计年龄比平均值低的女朋友有几个,并把他们的信息打印出来
答案:
[content_hide]
public class girlFriend {
private String name;
private int age;
private char gender;
private String hobby;
public girlFriend() {
}
public girlFriend(String name, int age, char gender, String hobby) {
this.name = name;
this.age = age;
this.gender = gender;
this.hobby = hobby;
}
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 char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
public class IT梦 {
public static void main(String[] args) {
girlFriend []arr = new girlFriend[4];
girlFriend g1 = new girlFriend("小美",18,'女',"打羽毛球");
girlFriend g2 = new girlFriend("小丽",21,'女',"瑜伽");
girlFriend g3 = new girlFriend("小爱",19,'女',"游泳");
girlFriend g4 = new girlFriend("小刘",22,'女',"爬山");
arr[0] = g1;
arr[1] = g2;
arr[2] = g3;
arr[3] = g4;
int age = g1.getAge() + g2.getAge() + g3.getAge() + g4.getAge();
int meanAge = age / arr.length;
System.out.println("平均年龄为:" + meanAge);
int count = 0;
for (int i = 0; i < arr.length; i++) {
girlFriend g = arr[i];
if(g.getAge() < meanAge){
count++;
System.out.println(g.getName() + ", " + g.getAge() + ", " + g.getGender() + ", " + g.getHobby());
}
}
System.out.println("比平均年龄低的一共有" + count + "个");
}
}
[/content_hide]
6.对象数组5
定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同学生的属性:学号,姓名,年龄要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断要求2:添加完毕后,遍历所有学生信息要求3:通过学号删除学生信息如果存在,则删除,如果不存在,则提示删除失败要求4:删除完毕后遍历所有学生信息要求5:查询数组学号为1的学生,如果存在,则他的年龄+1岁
答案:
[content_hide]
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;
}
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 class IT梦 {
public static void main(String[] args) {
student[] arr = new student[3];
student s1 = new student(1,"小明",18);
student s2 = new student(2,"小亮",19);
student s3 = new student(3,"小鹏",20);
arr[0] = s1;
arr[1] = s2;
arr[2] = s3;
student s4 = new student(4,"小张",19);
boolean flag = decideId(arr,s4.getId());
if(flag){
System.out.println("该id已存在,请重新输入id后添加");
}else {
int count = getcount(arr);
if(count == arr.length){
student[] newArr = getNewArr(arr);
newArr[count] = s4;
System.out.println("添加后的所有学生信息");
printArr(newArr);
}else {
arr[count] = s4;
System.out.println("添加后的所有学生信息");
printArr(arr);
}
}
int index = getIndex(arr,2);
if(index >= 0){
arr[index] = null;
System.out.println("------------------");
System.out.println("删除后的所有学生信息:");
printArr(arr);
}else {
System.out.println("删除失败");
}
int newIndex = getIndex(arr,1);
if(newIndex >= 0){
student s = arr[newIndex];
int newAge = s.getAge() + 1;
s.setAge(newAge);
System.out.println("------------------");
System.out.println("修改年龄完后的所有学生信息:");
printArr(arr);
}else {
System.out.println("当前id不存在,无法修改");
}
}
public static int getIndex(student[] arr,int id){
for (int i = 0; i < arr.length; i++) {
student s = arr[i];
if(s != null){
if(s.getId() == id){
return i;
}
}
}
return -1;
}
public static void printArr(student[] arr){
for (int i = 0; i < arr.length; i++) {
student s = arr[i];
if(s != null){
System.out.println(s.getId() + ", " + s.getName() + ", " + s.getAge());
}
}
}
public static student[] getNewArr(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 int getcount(student[] arr){
int count = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i] != null){
count++;
}
}
return count;
}
public static boolean decideId(student[] arr , int id){
for (int i = 0; i < arr.length; i++) {
student s = arr[i];
if(s != null){
if(s.getId() == id){
return true;
}
}
}
return false;
}
}
[/content_hide]
© 版权声明
THE END
暂无评论内容