<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                >[danger] ##### 文字版格斗游戲 * 需求:格斗游戲,每個游戲角色的姓名,血量,都不相同,在選定人物的時候(new對象的時候),這些信息就應該被確定下來。 * 舉例: 程序運行之后結果為: 姓名為:喬峰 血量為:100 姓名為:鳩摩智 血量為:100 喬峰舉起拳頭打了鳩摩智一下,造成了XX點傷害,鳩摩智還剩下XXX點血。 鳩摩智舉起拳頭打了鳩摩智一下,造成了XX點傷害,喬峰還剩下XXX點血。 喬峰舉起拳頭打了鳩摩智一下,造成了XX點傷害,鳩摩智還剩下XXX點血。 鳩摩智舉起拳頭打了鳩摩智一下,造成了XX點傷害,喬峰還剩下XXX點血。 喬峰K.O.了鳩摩智 * **小知識點`printf` 可以用 `%s` 進行占位** ***** * Role 創建角色類 ~~~ import java.util.Random; public class Role { private String name; // 名字 private int blood; // 血量 private char gender; // 性別 private String face;// 長相是隨機的 // 男的長相 String[] boyfaces = { "風流俊雅", "氣宇軒昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊涂", "面目猙獰" }; // 女生長相 String[] girlfaces = { "美奐絕倫", "沉魚落雁", "婷婷玉立", "身材嬌好", "相貌平平", "相貌簡陋", "慘不忍睹" }; // attack 攻擊描述: String[] attacks_desc = { "%s使出了一招【背心釘】,轉到對方的身后,一掌向%s背心的靈臺穴拍去。", "%s使出了一招【游空探爪】,飛起身形自半空中變掌為抓鎖向%s。", "%s大喝一聲,身形下伏,一招【劈雷墜地】,捶向%s雙腿。", "%s運氣于掌,一瞬間掌心變得血紅,一式【掌心雷】,推向%s。", "%s陰手翻起陽手跟進,一招【沒遮攔】,結結實實的捶向%s。", "%s上步搶身,招中套招,一招【劈掛連環】,連環攻向%s。" }; // injured 受傷描述: String[] injureds_desc = { "結果%s退了半步,毫發無損", "結果給%s造成一處瘀傷", "結果一擊命中,%s痛得彎下腰", "結果%s痛苦地悶哼了一聲,顯然受了點內傷", "結果%s搖搖晃晃,一跤摔倒在地", "結果%s臉色一下變得慘白,連退了好幾步", "結果『轟』的一聲,%s口中鮮血狂噴而出", "結果%s一聲慘叫,像灘軟泥般塌了下去" }; // 創建構造函數 public Role() { } public Role(String name, int blood, char gender) { this.name = name; this.blood = blood; this.gender = gender; this.setFace(gender); } public String getFace() { return face; } public void setFace(char gender) { Random r = new Random(); if (gender == '女') { int index = r.nextInt(girlfaces.length); this.face = girlfaces[index]; } else if (gender == '男') { int index = r.nextInt(boyfaces.length); this.face = boyfaces[index]; } else { this.face = "面目猙獰"; } } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public int getBlood() { return blood; } public void setBlood(int blood) { this.blood = blood; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } // 打印個人信息 public void showRoleInfo() { System.out.printf("姓名為%s 血量為%s 性別為%s 長相為 %s", getName(), getBlood(), getGender(), getFace()); System.out.println(); } // 定義一個攻擊方法 // 要攻擊的是對方因此 需要參數知道攻擊 對象 public void attack(Role role) { // 隨機扣除對方血量 Random r = new Random(); // 隨機釋放招數 int index = r.nextInt(attacks_desc.length); String KungFu = attacks_desc[index]; // 輸出一個攻擊的效果 System.out.printf(KungFu, this.getName(), role.getName()); System.out.println(); // 計算造成的傷害 1 ~ 20 int hurt = r.nextInt(20) + 1; // 剩余血量 int remainBoold = role.getBlood() - hurt; // 對剩余血量做一個驗證,如果為負數了,就修改為0 remainBoold = remainBoold < 0 ? 0 : remainBoold; // 修改一下挨揍的人的血量 role.setBlood(remainBoold); // 受傷的描述 // 血量> 90 0索引的描述 // 80 ~ 90 1索引的描述 // 70 ~ 80 2索引的描述 // 60 ~ 70 3索引的描述 // 40 ~ 60 4索引的描述 // 20 ~ 40 5索引的描述 // 10 ~ 20 6索引的描述 // 小于10的 7索引的描述 if (remainBoold > 90) { System.out.printf(injureds_desc[0], role.getName()); } else if (remainBoold > 80 && remainBoold <= 90) { System.out.printf(injureds_desc[1], role.getName()); } else if (remainBoold > 70 && remainBoold <= 80) { System.out.printf(injureds_desc[2], role.getName()); } else if (remainBoold > 60 && remainBoold <= 70) { System.out.printf(injureds_desc[3], role.getName()); } else if (remainBoold > 40 && remainBoold <= 60) { System.out.printf(injureds_desc[4], role.getName()); } else if (remainBoold > 20 && remainBoold <= 40) { System.out.printf(injureds_desc[5], role.getName()); } else if (remainBoold > 10 && remainBoold <= 20) { System.out.printf(injureds_desc[6], role.getName()); } else { System.out.printf(injureds_desc[7], role.getName()); } System.out.println(); } } ~~~ * 游戲執行類 ~~~ public class GameTest { public static void main(String[] args) { Role r1 = new Role("喬峰", 100, '男'); // 2.創建第二個角色 Role r2 = new Role("鳩摩智", 100, '男'); // 展示一下角色的信息 r1.showRoleInfo(); r2.showRoleInfo(); // 開始對打 while (true) { r1.attack(r2); // 如果血量沒有了 說明結束 if (r2.getBlood() == 0) { System.out.println(r1.getName() + " K.O了" + r2.getName()); break; } // r2開始攻擊r1 r2.attack(r1); if (r1.getBlood() == 0) { System.out.println(r2.getName() + " K.O了" + r1.getName()); break; } } } } ~~~ >[danger] ##### 創建數組將商品對象保存 * 需求: 定義數組存儲3個商品對象。 商品的屬性:商品的id,名字,價格,庫存。 創建三個商品對象,并把商品對象存入到數組當中。 ~~~ public class Goods { private String name; private double price; private int count; public Goods(String name, double price, int count) { this.name = name; this.price = price; this.count = count; } public Goods() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } } ~~~ * 打印商品 ~~~ public class GoodsTest { public static void main(String[] args) { Goods g1 = new Goods("手機", 1200, 2); Goods g2 = new Goods("汽車", 120000, 2); Goods g3 = new Goods("飛機", 12000010, 12); // 創建數組容器 Goods[] goodsLs = new Goods[3]; goodsLs[0] = g1; goodsLs[1] = g2; goodsLs[2] = g3; // 打印商品 for (int i = 0; i < goodsLs.length; i++) { Goods goods = goodsLs[i]; System.out.println(goods.getName() + goods.getPrice() + " " + goods.getCount()); } } } ~~~ >[danger] ##### 手動錄入商品 * 需求: 定義數組存儲3部汽車對象。 汽車的屬性:品牌,價格,顏色。 創建三個汽車對象,數據通過鍵盤錄入而來,并把數據存入到數組當中。 要求,計算出最后的平均價格 ~~~ public class Car { private String brand;// 品牌 private int price;// 價格 private String color;// 顏色 public Car(String brand, int price, String color) { this.brand = brand; this.price = price; this.color = color; } public Car() { } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } ~~~ * 保存Car ~~~ import java.util.Scanner; public class CarTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Car[] carLs = new Car[3]; for (int i = 0; i < carLs.length; i++) { Car car = new Car(); System.out.println("請輸入汽車的品牌"); String brand = sc.next(); car.setBrand(brand); System.out.println("請輸入汽車的價格"); int price = sc.nextInt(); car.setPrice(price); System.out.println("請輸入汽車的顏色"); String color = sc.next(); car.setColor(color); carLs[i] = car; // 保存到數組中 } // 計算平均加個 int sum = 0; for (int i = 0; i < carLs.length; i++) { sum += carLs[i].getPrice(); } double avg2 = sum * 1.0 / carLs.length; System.out.println(avg2); } } ~~~ >[danger] ##### 學生管理系統 定義一個長度為3的數組,數組存儲1~3名學生對象作為初始數據,學生對象的學號,姓名各不相同。 學生的屬性:學號,姓名,年齡。 要求1:再次添加一個學生對象,并在添加的時候進行學號的唯一性判斷。 要求2:添加完畢之后,遍歷所有學生信息。 要求3:通過id刪除學生信息 ? 如果存在,則刪除,如果不存在,則提示刪除失敗。 要求4:刪除完畢之后,遍歷所有學生信息。 要求5:查詢數組id為“heima002”的學生,如果存在,則將他的年齡+1歲 * 創建學生類 ~~~ public class Student { private String name; private int age; private int id; public Student(String name, int age, int id) { this.name = name; this.age = age; this.id = id; } public Student() { } 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; } } ~~~ * 創建操作 ~~~ public class StudentTest { public static void main(String[] args) { // 創建一個Student 數組 Student[] studentLs = new Student[3]; // 創建Student 對象 并保存進數組中 Student student1 = new Student("WW", 12, 1); Student student2 = new Student("CC", 12, 2); studentLs[0] = student1; studentLs[1] = student2; // 判讀創建新學生是否存在 Student student3 = new Student("CC", 12, 3); boolean flag = contains(studentLs, student3); if (flag) { System.out.print("當前數據已存在"); } else { // 如果不存在,將數據保存在數組中 // 但如果數組已經滿了 需要擴容增加新的位置 int index = getCount(studentLs); if (index == studentLs.length) { // 此時數組已經存滿 需要擴容 Student[] newStuLs = createNewArr(studentLs); newStuLs[index] = student3; // 要求2:添加完畢之后,遍歷所有學生信息。 printArr(newStuLs); } } } // 增加年齡 public static void addAge(Student[] arr, int id) { // 4.先要找到id為2的學生對于的索引 int index = getIndex(arr, id); // 5.判斷索引 if (index >= 0) { // 存在, 則將他的年齡+1歲 Student stu = arr[index]; // 把原來的年齡拿出來 int newAge = stu.getAge() + 1; // 把+1之后的年齡塞回去 stu.setAge(newAge); // 遍歷數組 printArr(arr); } else { // 不存在,則直接提示 System.out.println("當前id不存在,修改失敗"); } } // 刪除學生 public static void deleStu(Student[] arr, int id) { // 先判斷是否存在 int index = getIndex(arr, id); if (index >= 0) { // 如果存在,則刪除 arr[index] = null; // 遍歷數組 printArr(arr); } else { // 如果不存在,則提示刪除失敗 System.out.println("當前id不存在,刪除失敗"); } } // 創建方法判讀當前學生對象是否存在數組中 public static boolean contains(Student[] arr, Student stu) { boolean flag = false; for (int i = 0; i < arr.length; i++) { Student student = arr[i]; if (student != null) { if (student.getId() != stu.getId()) { flag = true; break; } } } return flag; } // 找到數組當前實際位置 public static int getCount(Student[] arr) { int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] != null) { count++; } } return count; } // 創建擴容student 數組 public static Student[] createNewArr(Student[] arr) { Student[] newArr = new Student[arr.length + 1]; for (int i = 0; i < arr.length; i++) { newArr[i] = arr[i]; } return newArr; } public static void printArr(Student[] arr) { for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; if (stu != null) { System.out.println(stu.getId() + ", " + stu.getName() + ", " + stu.getAge()); } } } public static int getIndex(Student[] arr, int id) { for (int i = 0; i < arr.length; i++) { // 依次得到每一個學生對象 Student stu = arr[i]; // 對stu進行一個非空判斷 if (stu != null) { int sid = stu.getId(); if (sid == id) { return i; } } } // 當循環結束之后,還沒有找到就表示不存在 return -1; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看