Java自学基础day8

%s是占位符号 

printf("%s非常漂亮","小猪");

1.文字格斗游戏代码(练习)
import java.util.Random;
public class Role {
    private String name;
    private int blood;
    private char gender;
    private String face;
    String[] boyfaces={"风流俊雅","气宇轩昂","一塌糊涂","面目狰狞"};
    String[] girlfaces={"美轮美奂","沉鱼落雁","亭亭玉立","身材娇好"};

    public char getGender() {
        return gender;
    }
    public void setGender(char gender) {
        this.gender = gender;
    }
    public String getFace(){
        return face;
    }

    public void setFace(char gender) {
        Random r=new Random();
        if(gender=='男'){
            int index=r.nextInt(boyfaces.length);
            this.face=boyfaces[index];
        } else if (gender=='女') {
            int index=r.nextInt(girlfaces.length);
            this.face=girlfaces[index];
        }else{
            this.face = "面目狰狞";
        }
    }

    public Role(){
    }

    public Role(String name, int blood, char gender) {
        this.name = name;
        this.blood = blood;
        this.gender = gender;
        //随机长相
        setFace(gender);
    }

    public String getName(){
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getBlood() {
        return blood;
    }
    public void setBlood(int blood) {
        this.blood = blood;
    }

    //方法的调用者去攻击参数
    public void attack(Role role){
        //计算造成的伤害1~20
        Random r=new Random();
        int hurt=r.nextInt(20)+1;
        //剩余血量
        int remainBoold=role.getBlood()-hurt;
        remainBoold=remainBoold<0?0:remainBoold;
        role.setBlood(remainBoold);
        System.out.println(this.name+"举起拳头,向"+role.getName()+"打了几下"+
                "造成了"+hurt+"点伤害"+role.getName()+"剩余血量为"+remainBoold);

    }
    public void showRoleInfo(){
        System.out.println("姓名为:"+getName());
        System.out.println("血量为:"+getBlood());
        System.out.println("性别为:"+getGender());
        System.out.println("颜值:"+getFace());
    }
}

主函数 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Role r1=new Role("猪猪侠",100,'男');
        Role r2=new Role("小鲤鱼",100,'女');
        r1.showRoleInfo();
        System.out.println("=========");
        r2.showRoleInfo();
        while (true){
            r1.attack(r2);
            if(r2.getBlood()==0){
                System.out.println("小鲤鱼被ko掉了!");
                break;
            }
            r2.attack(r1);
            if(r1.getBlood()==0){
                System.out.println("猪猪侠被ko掉了!");
                break;
            }
        }

    }
}

面向对象这块掌握的变更不是很好, 小练习并不是很会写。这个练习中setFace()方法里利用gender对setFace的处理运用的很巧妙。

2.面向对象综合案例

 键盘录入数据时,两套体系不可混用。因为第一套体系下输入123 123,而   123会被第二套体系接收

①.nextInt()接收整数。      ②.nextDouble() 接受小数。    ③.next()接受字符串

<使用第一套体系时,遇到空格,制表符,回车就会停止接受。>

①nextLine()

<属于第二套体系,只有遇到回车才会停止接收>

先创建javabean对象,再在主函数中调用。

//javabean类

public class Cars {
private String type;
private double price;
private String color;
    public Cars() {
    }
    public Cars(String type, double price, String color) {
        this.type = type;
        this.price = price;
        this.color = color;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    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;
    }
}

//main函数

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //创建一个数组
        Cars[] arr=new Cars[3];

        //键盘录入
        //第一套规则
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            Cars c=new Cars();
            System.out.println("请输入汽车的品牌");
            String brand=sc.next();
            c.setType(brand);
            System.out.println("请输入汽车的价格");
            Double price=sc.nextDouble();
            c.setPrice(price);
            System.out.println("请输入汽车的颜色");
            String color=sc.next();
            c.setColor(color);
            arr[i]=c;
        }
        //遍历
        for (int i = 0; i <arr.length ; i++) {
            Cars c=arr[i];
            System.out.println(c.getType()+","+c.getColor()+","+c.getPrice());
        }
    }
}

         Cars c=new Cars();语句不可以写在循环外,因为循环外的car对象只创建一次对象。   

3.面向对象超级综合案例

public class Main {
    public static void main(String[] args) {
        //创建一个数组
        Students[] arr=new Students[3];
        Students stu1=new Students("小花",5,001);
        Students stu2=new Students("小耶",15,002);
        arr[0]=stu1;
        arr[1]=stu2;

        //再次添加一个学生对象
        Students stu3=new Students("小花树",15,003);
        boolean flag=contains(arr,stu3.getId());
        if(flag){
            System.out.println("当前id重复,请改进id后在进行添加");
        }else{
            if(getCount(arr)==arr.length){
                //数组已满,扩容
                Students[] arr2=createnweArr(arr);
                arr2[getCount(arr)+1]=stu3;
                //遍历数组
                printArr(arr2);
            }else{
                //数组未满,直接添加到数组中(count获取到的是数组的数据长度,就是添加到2索引)
                arr[getCount(arr)]=stu3;
                //遍历数组
                printArr(arr);
            }
        }
        //删除数组信息
        boolean flag1=deleteArr(arr,002);
        if(flag1==true){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        //遍历所有数组
        printArr(arr);

        //查询数组id为"heima002"的信息
        int index=queryArr(arr,001);
        if(index==-1){
            System.out.println("查询失败,没有该学生信息");
        }else{
            System.out.println("查询成功,该学生年龄如下:");
            System.out.println( arr[index].getAge()+1);
        }

    }
    //查询数组id为"heima002"的信息
   public  static int queryArr(Students[] arr,int id){
       for (int i = 0; i < arr.length; i++) {
           if(arr[i]!=null&&arr[i].getId()==id){
               return i;
           }
       }
       return -1;
   }

    //删除数组信息
    public static boolean deleteArr(Students[] arr,int id){
        for (int i = 0; i <arr.length ; i++) {
            if(arr[i]!=null&&arr[i].getId()==id) {
                    arr[i] = null;
                    return true;
            }
        }
        return false;
    }

    //遍历数组
    public static void printArr(Students[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println(arr[i].getName() + "," + arr[i].getAge() + "," + arr[i].getId());
            }
        }
    }

    //创建一个新数组
    public static Students[] createnweArr(Students[] arr){
        //创建一个新数组
        Students[] arr1=new Students[arr.length+1];
        for (int i = 0; i < arr1.length ; i++) {
            arr1[i]=arr[i];
        }
        return arr1;
    }
    //数组是否满了的状态
    public  static int getCount(Students[] arr){
        int count=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }
    //唯一性的判断
    public static boolean contains(Students[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            //获取数组对象中每一个学生对象
            Students stu=arr[i];
            //获取数组对象中学生对象的id
            if(stu!=null){
            int sid= stu.getId();
                //比较两个id
                if(sid==id){
                    return true;
                }
            }
        }
        return false;
    }
}
//配套的学生对象
public class Students {
private String name;
private int age;
private int id;   //学号
    public Students() {
    }
    public Students(String name, int age, int id) {
        this.name = name;
        this.age = age;
        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 int getId() {
        return id;}

    public void setId(int id) {
        this.id = id;
    }
}

①.Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Students.getId()" because "stu" is null  

运行过程中,出现了如上报错。原因是,Students stu=arr[i]; stu获取数组对象中学生对象的id,但是数组中存在null了,而用null去调用方法注定会报错 int sid= stu.getId();。所以在此处加个判断就好。

②.在解决问题5时,查询不出来的时情况,返回值赋予了0,没有考虑到数组中存在的0索引,i的返回值也可能为0,导致冲突了。查询的时的结果一直为查询失败。好在最后发现了。

③.代码编写过程中仍然不够规范,需要注释,以及一些常量符号的滥用。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值