1,题目大概:
import java.util.Random;
public class testRole1 {
public static void main(String[] args) {
Role r1 = new Role("小明",10,'男');
Role r2 = new Role("小刚",6,'男');
while(true) {
r1.attack(r2);
if(r2.getBlood()==0){
System.out.println(r1.getName()+"K.O"+r2.getName());
break;
}
r2.attack(r1);
if(r1.getBlood()==0){
System.out.println(r2.getName()+"K.O"+r1.getName());
break;
}
}
}
}
public class Role {
private String name;
private int blood;
private char gender;
private String face;
String[] attack_desc = {"%s使出降龙十八掌,一掌打去 %s","%s使出九阴白骨爪,打向%s"};
String[] boyFace = {"玉树临风","丑八怪"};
String[] girlFace = {"美若天仙","相貌平平"};
public Role() {
}
public Role(String name,int blood,char gender){
this.name = name;
this.blood = blood;
this.gender = gender;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getFace() {
return face;
}
public void setFace(String face) {
Random r = new Random();
if(getGender()=='女')
{
int index = r.nextInt(boyFace.length);//这样的程序可变性高
this.face = girlFace[index];
} else if (getGender()=='男') {
int index = r.nextInt(girlFace.length);
this.face = girlFace[index];
}
}
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(Role role){
Random r = new Random();
int hurt = r.nextInt(20)+1;
int remainBlood = role.blood-hurt;
remainBlood = (remainBlood>0)?remainBlood:0;//血量剩余
role.setBlood(remainBlood);//这里要把原来的血量给重新赋回,否则blood一直是原来的值,最后造成在test测试类中一直循环
int index = r.nextInt(attack_desc.length);
String kungFu = attack_desc[index];
System.out.printf(kungFu,this.getName(),role.getName());
System.out.println(this.getName()+"打了"+role.getName()+"一拳,"+role.getName()+"造成了"+ hurt+"伤害," +
role.getName()+"还剩"+remainBlood+"血");
}
}
2,
public class goodsTest {
public static void main(String[] args) {
goods[] Goods = new goods[3];
goods g1 = new goods(1,"huawei",555.2,48);
goods g2 = new goods(2,"apple",5556.2,45);
goods g3 = new goods(3,"xioami",156.3,45);
Goods[0] = g1;
Goods[1] = g2;
Goods[2] = g3;
for (int i = 0; i < Goods.length ; i++) {
goods good = Goods[i];//用good来接受数组Goods遍历的每一元素
System.out.println(good.getId()+","+good.getName()+","+good.getPrice()+","+good.getNum());
}
}
}
public class goods {
private int id;
private String name;
private double price;
private int num;
public goods() {
}
public goods(int id, String name, double price, int num) {
this.id = id;
this.name = name;
this.price = price;
this.num = num;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
3,
import java.util.Scanner;
public class carTest {
public static void main(String[] args) {
car[] Car = new car[3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < Car.length ; i++) {
car c = new car();//注意这里的new car(),它要放在循环里,不能放在for循环外,否则打印的数全部一样
System.out.println("please input brand");
String brand = sc.next();
c.setBrand(brand);
System.out.println("please input price");
int price = sc.nextInt() ;
c.setPrice(price);
System.out.println("please input color");
String color = sc.next();
c.setColor(color);
Car[i] = c;//这里要把c接到的信息存入数组中!
}
for (int i = 0; i < Car.length ; i++) {
car s = Car[i];
System.out.println(s.getBrand()+s.getPrice() +s.getColor() );
}
}
}
public class car {
private String brand;
private int price;
private String color;
public car() {
}
public car(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;
}
}
4,
public class phoneTest {
public static void main(String[] args) {
phone[] p = new phone[3];
double avg;
int sum = 0;
phone p1 = new phone("huawei",2322,"green");
phone p2 = new phone("xiaomi",8555,"red");
phone p3 = new phone("sanxing",2822,"pink");
p[0] = p1;
p[1] = p2;
p[2] = p3;
for (int i = 0; i < p.length; i++) {
phone temp = p[i];
sum+= temp.getPrice();
}
avg = sum *1.0/p.length;//这里为了得到小数,sum注意要乘以1.0
System.out.println(avg);
}
}
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;
}
}