关于java:那些年我们一起做过的-Java-课后练习题56-60

11次阅读

共计 5340 个字符,预计需要花费 14 分钟才能阅读完成。

本文已同步至:村雨遥

实例 56

题目

设计一个配备类 Equipment,该类有两个属性,一个是名字 name,类型为字符串类型,另一个是价格 price,类型为 int。而后实例化 3 件具体配备并打印其名字和价格。

剖析

次要考查类的创立以及如何实例化一个对象,并且随同有重写 toString() 办法。

实现

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 村雨
 * @version : 1.0
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example56
 * @createTime : 2021/7/3 22:23
 * @email : 747731461@qq.com
 * @公众号 : 村雨遥
 * @website : https://cunyu1943.github.io
 * @description :
 */
public class Example56 {public static void main(String[] args) {Equipment equipment1 = new Equipment("饮血之刃", 1500);
        Equipment equipment2 = new Equipment("破军", 2000);
        Equipment equipment3 = new Equipment("攻速鞋", 500);

        System.out.println("伽罗目前的配备为:");
        System.out.println(equipment1.toString());
        System.out.println(equipment2.toString());
        System.out.println(equipment3.toString());
    }
}

class Equipment {
    private String name;
    private int price;

    public Equipment() {}

    public Equipment(String name, int price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {return "配备名:" + name + ", 价格:" + price;}
}

后果

实例 57

题目

当初王者光荣很火,置信大部分人也玩过,那咱们就来定义一个英雄类,用来作为王者光荣里的各英雄的父类。一般来讲,一个英雄有名字、血量、蓝量、初始挪动速度、攻打值……,咱们须要做的,就是将这些作为类的属性尽可能地增加到类中。

剖析

次要考查如何定义类,以及如何增加类中的属性,而且如何抉择属性的数据类型。

实现

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 村雨
 * @version : 1.0
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example57
 * @createTime : 2021/7/4 8:40
 * @email : 747731461@qq.com
 * @公众号 : 村雨遥
 * @website : https://cunyu1943.github.io
 * @description :
 */
public class Example57 {public static void main(String[] args) {Hero hero = new Hero("虞姬", 3000, 1000, 50, 800, 0, 0, 0);
        System.out.println("英雄信息如下");
        System.out.println(hero.toString());
    }
}

class Hero {
    private String name;
    private float hp;
    private float mp;
    private int initSpeed;
    private int attack;
    private int killed;
    private int beKilled;
    private int assist;

    public Hero() {}

    public Hero(String name, float hp, float mp, int initSpeed, int attack, int killed, int beKilled, int assist) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
        this.initSpeed = initSpeed;
        this.attack = attack;
        this.killed = killed;
        this.beKilled = beKilled;
        this.assist = assist;
    }


    @Override
    public String toString() {final StringBuffer sb = new StringBuffer();
        sb.append("名字 ='").append(name).append('\'');
        sb.append(", 血量 =").append(hp);
        sb.append(", 蓝量 =").append(mp);
        sb.append(", 初始速度 =").append(initSpeed);
        sb.append(", 攻打值 =").append(attack);
        sb.append(", 击杀数 =").append(killed);
        sb.append(", 被击杀数 =").append(beKilled);
        sb.append(", 助攻数 =").append(assist);
        return sb.toString();}
}

后果

实例 58

题目

既然王者光荣中的英雄有很多属性,而且也有很多配备,咱们买了配备之后就会给咱们的英雄加血量、加攻打值或则加挪动速度之类。咱们就来定义几个办法,用于购买配备后给咱们的英雄减少属性值。

剖析

次要考查如何给咱们的类定义方法。

实现

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 村雨
 * @version : 1.0
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example58
 * @createTime : 2021/7/4 8:58
 * @email : 747731461@qq.com
 * @公众号 : 村雨遥
 * @website : https://cunyu1943.github.io
 * @description :
 */
public class Example58 {public static void main(String[] args) {Hero hero = new Hero("虞姬", 3000, 1000, 50, 800, 0, 0, 0);
        System.out.println("英雄初始信息如下");
        System.out.println(hero.toString());
        hero.addAttack(1000);
        hero.addSpeed(100);
        hero.addKilled(5);

        System.out.println("英雄减少属性后信息如下");
        System.out.println(hero.toString());
    }
}

class Hero {
    private String name;
    private float hp;
    private float mp;
    private int initSpeed;
    private int attack;
    private int killed;
    private int beKilled;
    private int assist;

    public Hero() {}

    public Hero(String name, float hp, float mp, int initSpeed, int attack, int killed, int beKilled, int assist) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
        this.initSpeed = initSpeed;
        this.attack = attack;
        this.killed = killed;
        this.beKilled = beKilled;
        this.assist = assist;
    }

    public void addSpeed(int add) {System.out.println("购买了鞋子");
        this.initSpeed += add;
    }

    public void addAttack(int add) {System.out.println("购买了攻打装");
        this.attack += add;
    }

    public void addKilled(int add) {System.out.println("你击杀了一名敌人");
        this.killed += add;
    }


    @Override
    public String toString() {final StringBuffer sb = new StringBuffer();
        sb.append("名字 ='").append(name).append('\'');
        sb.append(", 血量 =").append(hp);
        sb.append(", 蓝量 =").append(mp);
        sb.append(", 初始速度 =").append(initSpeed);
        sb.append(", 攻打值 =").append(attack);
        sb.append(", 击杀数 =").append(killed);
        sb.append(", 被击杀数 =").append(beKilled);
        sb.append(", 助攻数 =").append(assist);
        return sb.toString();}
}

后果

实例 59

题目

设计一个办法,用于计算你的 BMI 值是多少,其中 BMI = 体重(kg)/ 身高(m)* 身高(m)

剖析

输出体重和身高,而后调用办法计算 BMI 即可。

实现

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 村雨
 * @version : 1.0
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example59
 * @createTime : 2021/7/4 9:46
 * @email : 747731461@qq.com
 * @公众号 : 村雨遥
 * @website : https://cunyu1943.github.io
 * @description :
 */
public class Example59 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        System.out.println("输出体重 kg");
        float weight = scanner.nextFloat();
        System.out.println("输出身高 m");
        float height = scanner.nextFloat();
        System.out.println("BMI =" + calcBMI(height, weight));
    }

    public static double calcBMI(float height, float weight) {return weight / height / height;}
}

后果

实例 60

题目

通过输出的月份,判断该月处于哪一个节令。

剖析

次要考查 switch 多分支的判断,而且要留神,要完结一个分支的判断时,须要有 break。要留神 switch 自从 JDK 1.7 及之后是反对 String 类型的。当然也能够应用 if 进行判断。

实现

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 村雨
 * @version : 1.0
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example60
 * @createTime : 2021/7/4 10:00
 * @email : 747731461@qq.com
 * @公众号 : 村雨遥
 * @website : https://cunyu1943.github.io
 * @description :
 */
public class Example60 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);

        System.out.println("输出月份");
        int month = scanner.nextInt();

        switch (month) {
            case 3:
            case 4:
            case 5:
                System.out.println(month + "月是秋季");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println(month + "月是冬季");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println(month + "月是秋季");
                break;
            case 12:
            case 1:
            case 2:
                System.out.println(month + "月是夏季");
                break;
            default:
                break;
        }
    }
}

后果

正文完
 0