Examples of 56

The title

Design an Equipment class Equipment. This class has two properties, one is name, the type is string, the other is price, the type is int. Then instantiate three specific pieces of equipment and print their names and prices.

Analysis of the

It looks at the creation of classes and how to instantiate an object, along with overriding the toString() method.

implementation

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example56
 * @createTime: 2021/7/3 makest *@email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: * /
public class Example56 {
    public static void main(String[] args) {
        Equipment equipment1 = new Equipment("Blade of Blood.".1500);
        Equipment equipment2 = new Equipment("Break the army".2000);
        Equipment equipment3 = new Equipment("Attack shoes.".500);

        System.out.println("Gello's current equipment is:"); 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(a) {}public Equipment(String name, int price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString(a) {
        return "Equipment Name:" + name + ", price:+ price; }}Copy the code

The results of

Examples of 57

The title

Now king of Glory is very popular, I believe that most people also played, then we will define a hero class, used as the father class of each hero in King of Glory. Generally speaking, a hero has a name, health, mana, initial movement speed, attack value… All we need to do is add these as class attributes as possible to the class.

Analysis of the

We look at how to define a class, how to add attributes to a class, and how to select the data type of the attributes.

implementation

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example57
 * @createTime: 2021/7/4 as *@email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: * /
public class Example57 {
    public static void main(String[] args) {
        Hero hero = new Hero("Concubine".3000.1000.50.800.0.0.0);
        System.out.println("Hero info is as follows"); 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(a) {}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(a) {
        final StringBuffer sb = new StringBuffer();
        sb.append("Name = '").append(name).append('\' ');
        sb.append(", blood =").append(hp);
        sb.append(", blue quantity =").append(mp);
        sb.append(", initial velocity =").append(initSpeed);
        sb.append(", attack value =").append(attack);
        sb.append(", kills =").append(killed);
        sb.append(", 被击杀数 = ").append(beKilled);
        sb.append(", assists =").append(assist);
        returnsb.toString(); }}Copy the code

The results of

Instance 58

The title

Since Kings of Honor heroes have a lot of stats, and they also have a lot of equipment, when we buy equipment, we give our heroes more health, attack value, movement speed, etc. Let’s define a few methods that we can use to add stats to our heroes after purchasing equipment.

Analysis of the

We’ll focus on how to define methods for our class.

implementation

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example58
 * @createTime: 2021/7/4 verily *@email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: * /
public class Example58 {
    public static void main(String[] args) {
        Hero hero = new Hero("Concubine".3000.1000.50.800.0.0.0);
        System.out.println("Hero initial info is as follows");
        System.out.println(hero.toString());
        hero.addAttack(1000);
        hero.addSpeed(100);
        hero.addKilled(5);

        System.out.println("Hero added stats as follows"); 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(a) {}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("Purchased shoes.");
        this.initSpeed += add;
    }

    public void addAttack(int add) {
        System.out.println("Purchased an attack suit.");
        this.attack += add;
    }

    public void addKilled(int add) {
        System.out.println("You killed an enemy.");
        this.killed += add;
    }


    @Override
    public String toString(a) {
        final StringBuffer sb = new StringBuffer();
        sb.append("Name = '").append(name).append('\' ');
        sb.append(", blood =").append(hp);
        sb.append(", blue quantity =").append(mp);
        sb.append(", initial velocity =").append(initSpeed);
        sb.append(", attack value =").append(attack);
        sb.append(", kills =").append(killed);
        sb.append(", 被击杀数 = ").append(beKilled);
        sb.append(", assists =").append(assist);
        returnsb.toString(); }}Copy the code

The results of

Examples of 59

The title

Design a method to calculate your BMI, where BMI = weight (kg)/height (m) * height (m)

Analysis of the

Enter your weight and height and then call the method to calculate BMI.

implementation

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example59
 * @createTime: 2021/7/4.so *@email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: * /
public class Example59 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input weight kg");
        float weight = scanner.nextFloat();
        System.out.println("Enter m for height");
        float height = scanner.nextFloat();
        System.out.println("BMI = " + calcBMI(height, weight));
    }

    public static double calcBMI(float height, float weight) {
        returnweight / height / height; }}Copy the code

The results of

Examples of 60

The title

The input month is used to determine the season of the month.

Analysis of the

A break is required to terminate the judgment of a branch. Note that The Switch has supported String since JDK 1.7 and later. Of course, you can also use if to determine.

implementation

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example60
 * @createTime : 2021/7/4 10:00
 * @email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: * /
public class Example60 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Input month");
        int month = scanner.nextInt();

        switch (month) {
            case 3:
            case 4:
            case 5:
                System.out.println(month + "The month is spring");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println(month + "The month is summer");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println(month + "The moon is autumn.");
                break;
            case 12:
            case 1:
            case 2:
                System.out.println(month + "The moon is winter");
                break;
            default:
                break; }}}Copy the code

The results of