基于JAVA的类与对象做出的英雄打怪兽程序
生活随笔
收集整理的這篇文章主要介紹了
基于JAVA的类与对象做出的英雄打怪兽程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、英雄打怪獸
基于Java類與對象的調用而做出的程序.
二、實例代碼
1.主函數
代碼如下:
menu();2.menu函數
代碼如下:
Scanner scanner = new Scanner(System.in);System.out.println("創建英雄界面,請輸入英雄的姓名");Hero hero=new Hero();hero.setHeroName(scanner.next());System.out.println("請輸入英雄的血量");hero.setHealth(scanner.nextInt());System.out.println("選擇一把武器吧");Weapon[] weapons=new Weapon[5];weapons[0]=new Weapon();weapons[0].setWeaponName("霜之哀傷");weapons[0].setAttack(4500);weapons[1]=new Weapon();weapons[1].setWeaponName("火之高興");weapons[1].setAttack(4000);weapons[2]=new Weapon();weapons[2].setWeaponName("尚方寶劍");weapons[2].setAttack(100);weapons[3]=new Weapon();weapons[3].setWeaponName("蘭德里的折磨");weapons[3].setAttack(2500);weapons[4]=new Weapon();weapons[4].setWeaponName("戰爭號令");weapons[4].setAttack(1000);System.out.println("武器名 攻擊力");for(int i=0;i<weapons.length;i++){weapons[i].setId(i+1);System.out.println(weapons[i]);}System.out.println("要獲得第幾把武器");int weaponnum=scanner.nextInt()-1;System.out.println(hero.heroName+"獲得了"+weapons[weaponnum].weaponName);System.out.println("現在可以挑戰怪獸了");Monster[] monsters=new Monster[3];System.out.println("怪物名 血量 類型");for(int i=0;i<monsters.length;i++){monsters[i]=new Monster();if(i==0){monsters[i].setMonsterName("雷德王");monsters[i].setMonsterHealth(3500);monsters[i].setType("原始異獸");}else if(i==1){monsters[i].setMonsterName("杰頓");monsters[i].setMonsterHealth(4500);monsters[i].setType("外星異獸");}else if(i==2){monsters[i].setMonsterName("嘎嘎");monsters[i].setMonsterHealth(1500);monsters[i].setType("外星人");}monsters[i].setId(i+1);System.out.println(monsters[i]);}System.out.println("挑戰第幾只");int monsternum = scanner.nextInt()-1;if(hero.health+weapons[weaponnum].attack>monsters[monsternum].monsterHealth){System.out.println(hero.heroName+"戰勝了boss呢");}else if(hero.health+weapons[weaponnum].attack==monsters[monsternum].monsterHealth){System.out.println(hero.heroName+"和"+monsters[monsternum].monsterName+"同歸于盡了呢");}else{System.out.println("勝敗乃兵家常事,大俠請重新來過");menu();}}3.Hero函數
代碼如下:
String heroName;int health;public void setHeroName(String heroName) {this.heroName = heroName;}public void setHealth(int health) {this.health = health;}public int getHealth() {return health;}public String getHeroName() {return heroName;}4.Monster函數
代碼如下:
public int id;public String monsterName,type;public int monsterHealth;public int getMonsterHealth() {return monsterHealth;}public void setId(int id) {this.id = id;}public int getId() {return id;}public String getType() {return type;}public String getMonsterName() {return monsterName;}public void setMonsterHealth(int monsterHealth) {this.monsterHealth = monsterHealth;}public void setMonsterName(String monsterName) {this.monsterName = monsterName;}public void setType(String type) {this.type = type;}public String toString(){return monsterName+"\t\t"+monsterHealth+"\t\t"+type;}5.Weapon函數
代碼如下:
public String weaponName;public int attack,id;public void setId(int id) {this.id = id;}public int getId() {return id;}public void setWeaponName(String weaponName) {this.weaponName = weaponName;}public void setAttack(int attack) {this.attack = attack;}public String getWeaponName() {return weaponName;}public int getAttack() {return attack;}public String toString(){return weaponName+"\t\t\t"+attack+"\t\t"+id;}實例圖。
總結
調用了類與對象的方法做出了這個程序
總結
以上是生活随笔為你收集整理的基于JAVA的类与对象做出的英雄打怪兽程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 当当网数据分析
- 下一篇: C语言之整数类型