文字版格斗游戏
public class people {
private String name;
private int xuetiao;
public people(String name){
this.name=name;
this.xuetiao=100;
}
public String getName(){
return this.name;
}
public int getXuetiao(){
return this.xuetiao;
}
}
import java.util.Random;
public class Xueliang {
public int xuetiaoxiaoshi(){
Random r=new Random();
int a=r.nextInt(1,101);
return a;
}
}
import java.util.Random;
public class fight {
public void ko(people p1,people p2){
int x1=p1.getXuetiao();//p1血条
int x2=p2.getXuetiao();//p2血条
Random r=new Random();
Xueliang xueliang =new Xueliang();
int i=r.nextInt();
while(true) {
if (i % 2 != 0) {
int a = xueliang.xuetiaoxiaoshi();//消失多少
x2 -= a;
System.out.println(p1.getName() + "举起拳头打了" +
p2.getName() + "一下造成了" + a + "伤害" + p2.getName() +
"还剩下" + x2 + "血量");
}//i为奇数p1出招
if (i % 2 == 0) {
int a = xueliang.xuetiaoxiaoshi();//消失多少
x1 -= a;
System.out.println(p2.getName() + "举起拳头打了" +
p1.getName() + "一下造成了" + a + "伤害" + p1.getName() +
"还剩下" + x1 + "血量");
}//i为偶数p2出招
if (x1 <= 0) {
System.out.println(p2.getName() + "KO了" + p1.getName());
break;
}//p1死结束
if (x2 <= 0) {
System.out.println(p1.getName() + "KO了" + p2.getName());
break;
}//p2死结束
i++;
}
}
}
public class test {
public static void main (String []args){
people p1=new people("小豫");
people p2=new people("小杰");
fight f1=new fight();
f1.ko(p1,p2);
}
商品
public class Shang_pin {
private int id;
private String name;
private double jiage;
private int kucun;
public Shang_pin set(int id,String name,double jiage,int kucun){
this.id=id;
this.name=name;
this.jiage=jiage;
this.kucun=kucun;
return this;
}
public int getid() {
return id;
}
public String getname() {
return name;
}
public double getjiage() {
return jiage;
}
public int getkucun() {
return kucun;
}
}
/*定义数组存储3个商品对象。
商品的属性:商品的id,名字,价格,库存。
创建三个商品对象,并把商品对象存入到数组当中。*/
public class test {
public static void main(String[]args){
Shang_pin S=new Shang_pin();
Shang_pin []arr=new Shang_pin[3];
arr[0]=S.set(1,"小米",1.5,50);
arr[1]=S.set(2,"黑豆",0.8,10);
arr[2]=S.set(3,"黄豆",0.65,37);
for(int i=0;i<arr.length;i++)
System.out.println("id:"+arr[i].getid()+" 商品名称:"+
arr[i].getname()+" 价格"+arr[i].getjiage()+" 库存"+arr[i].getkucun());
}
}
汽车
/*定义数组存储3部汽车对象。
汽车的属性:品牌,价格,颜色。
创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。*/
public class car {
private String pinpai;
private double jiage;
private String yanse;
public void setpinpai(String pinpai){
this.pinpai=pinpai;
}
public String getpinpai(){
return pinpai;
}
public void setjiage(double jiage){
this.jiage=jiage;
}
public double getjiage(){
return jiage;
}
public void setyanse(String yanse){
this.yanse=yanse;
}
public String getyanse(){
return yanse;
}
}
import java.util.Scanner;
public class test {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
car C=new car();
car []arr=new car[3];
for(int i=0;i<arr.length;i++){
arr[i]=C;
System.out.print("请输入车辆品牌");
arr[i].setpinpai(sc.next());
System.out.print("请输入车辆价格");
arr[i].setjiage(sc.nextInt());
System.out.print("请输入车辆颜色");
arr[i].setyanse(sc.next());
}
for(int i=0;i<arr.length;i++ ){
System.out.println("车辆品牌"+arr[i].getpinpai()+
"车辆价格"+arr[i].getjiage()+
"车辆颜色"+arr[i].getyanse());
}
}
}
手机
/*定义数组存储3部手机对象。
手机的属性:品牌,价格,颜色。
要求,计算出三部手机的平均价格*/
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 test {
public static void main(String[] args) {
phone[] phones = new phone[3];
phones[0] = new phone("小米", 1999, "黑色");
phones[1] = new phone("华为", 2999, "白色");
phones[2] = new phone("苹果", 3999, "红色");
int sum = 0;
for (int i = 0; i < phones.length; i++) {
sum += phones[i].getPrice();
}
System.out.println("三部手机的平均价格为:" + sum / phones.length);
}
女朋友
/*定义数组存储4个女朋友的对象
女朋友的属性:姓名,年龄,性别,爱好
要求1:计算出四女朋友的平均年龄
要求2:统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来。*/
public class girl {
private String name;
private int age;
private char xingbie;
private String aihao;
public void setgirl(String name,int age,char xingbie,String aihao){
this.name=name;
this.age=age;
this.xingbie=xingbie;
this.aihao=aihao;
}
public String getname(){
return name;
}
public int getage(){
return age;
}
public char getxingbie(){
return xingbie;
}
public String aihao(){
return aihao;
}
}
public class test {
public static void main(String[]args){
girl [] girlfriend=new girl[4];
for(int i=0;i<girlfriend.length;i++){
girlfriend[i]=new girl();
}
girlfriend[0].setgirl("张一",213,'男',"玩lol");
girlfriend[1].setgirl("张二",18,'男',"玩lo");
girlfriend[2].setgirl("张三",37,'男',"玩lo");
girlfriend[3].setgirl("张四",26,'男',"玩l");
int count=0,sum=0;
for(int i=0;i<girlfriend.length;i++){
sum+=girlfriend[i].getage();
}
sum/=girlfriend.length;
for(int i=0;i<girlfriend.length;i++)
{
if(girlfriend[i].getage()<sum){
count++;
System.out.println(girlfriend[i].getname()+girlfriend[i].getage()
+girlfriend[i].getxingbie()+girlfriend[i].aihao());
}
}
System.out.println("年龄比平均值低的女朋友有"+count+"个");
}
}
标签:return,String,int,练习,private,面向对象,new,public,综合 From: https://blog.csdn.net/weixin_68931360/article/details/144972556