Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to create a hero class and monster class in LOL

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Java to create a hero and monster in LOL". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use Java to create a hero and monster in LOL"!

The first problem of Java exercises

Topic: create a hero class and monster class in LOL, which requires:

Add health attribute and rank attribute to monster class, health value = level * 1000

Add "experience", "grade", "attack power" and other attribute variables to the hero class, and add the "hit" method. The target is the monster, and the result is the monster's health-hero attack power.

Test points: class declaration, method declaration, object declaration and property invocation

Hero class

Public class Hero {int EXP;// experience value int grade;// level int aggressivity;// attack / / hit method int strike (int x) {return x-aggressivity;} public int getEXP () {return EXP } public void setEXP (int EXP) {this.EXP = EXP;} public int getGrade () {return grade;} public void setGrade (int grade) {this.grade = grade;} public int getAggressivity () {return aggressivity } public void setAggressivity (int aggressivity) {this.aggressivity = aggressivity;}}

Monster class

Public class Monster {int grade;// level int healthValue; / / Life value public int getGrade () {return grade;} public void setGrade (int grade) {this.grade = grade;} public int getHealthValue () {healthValue = grade*1000; return healthValue } public void setHealthValue (int healthValue) {this.healthValue = healthValue;}}

Main

Public class Work1 {public static void main (String [] args) {/ / monster attribute Monster m = new Monster (); m.setGrade (5); System.out.println ("monster level" + m.grade+ "level health is:" + m.getHealthValue ()); / / hero Hero h = new Hero (); h.setAggressivity (160); int a A = h.strike (m.healthValue); m.setHealthValue (a); System.out.println ("Hero hit Health:" + m.healthValue);}} Java exercise 2

Topic: customize a hero class, requirements:

The lowest level of hero is 0, the highest level is 30, the lowest experience value is 0, and the highest experience value is 30 level.

A constructor with parameters is required in this class. The parameter is the experience value of the hero. The experience value should be within the required range during initialization. The hero level is calculated by the experience value. The hero level formula is as follows: n = current level, EXP (experience value) = 30 (N ^ 3 + 5N)-80.

Build a no-parameter constructor and set the experience value to 0

The hero object is constructed by using the hero class with no parameter and the constructor with parameters, and the hero's level and experience value are output.

Test points: class declaration, constructor, use of this, method invocation

Hero class

Public class Hero2 {int EXP;// empirical value int grade;// level / / Parametric constructor public Hero2 (int EXP) {this.EXP = EXP;// empirical value} / / No parameter constructor public Hero2 () {EXP=0;// empirical value}}

Main

Public class Work2 {public static void main (String [] args) {/ / Parametric construction result Hero2 e = new Hero2 (); System.out.print ("parameterless construction"); System.out.println ("experience value: + e.EXP+", grade: "+ e.grade+"); / / parametric construction result int aq120000 Hero2 h = new Hero2 (a); System.out.print ("parametric construction"); for (int I = 1; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report