那些年,我们一起做过的 Java 课后练习题(56 - 60)
實例 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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;}} }結果
總結
以上是生活随笔為你收集整理的那些年,我们一起做过的 Java 课后练习题(56 - 60)的全部內容,希望文章能夠幫你解決所遇到的問題。