日期:2014-05-20 浏览次数:20708 次
package com.hm; import java.util.Random; public class Main { private void show() { Hero hero = new Hero(); hero.weaponType = 3; Monster monster = new Monster(); monster.blood = 1000; while (monster.blood > 0) attack(hero, monster); } private void attack(Hero hero, Monster monster) { String weaponName = ""; int power = 0; boolean crit = new Random().nextBoolean(); switch (hero.weaponType) { case 1: weaponName = "木剑"; power = crit ? 20 * 2 : 20; break; case 2: weaponName = "铁剑"; power = crit ? 50 * 2 : 50; break; case 3: weaponName = "魔剑"; boolean b = new Random().nextBoolean(); power = crit ? 100 * 2 : 100; break; default: break; } monster.blood -= power; if (monster.blood > 0) System.out.println((crit ? "暴击!" : "") + "玩家使用" + weaponName + "对怪物造成了" + power + "点伤害!"); else System.out.println("怪物被击毙"); } class Hero {public int weaponType;} class Monster {public int blood;} }
------解决方案--------------------
思路如下,直接已经4个类了,70行写不完,我是菜鸟……
Hero hero = HeroFactory.getHero(); Monstor monstor = MonstorFactory.getMonstor(); while(monstor.isAlive()){ AttackAttribute attr = hero.attack(); monstor.getattack(attr); }
------解决方案--------------------
package test; import java.util.Random; public class Main{ public void show(){ Hero h = new Hero(); Monster monster = new Monster(); monster.blood=1000; h.attack(0, monster); } class Hero { public String[] weapon={"木剑","铁剑","魔剑"}; public int [] power = {20,50,100}; public void attack(int weaponType,Monster monster){ boolean crit = new Random().nextBoolean(); monster.blood -=power[weaponType]; if (monster.blood > 0){ System.out.println((crit ? "暴击!" : "") + "玩家使用" + weapon[weaponType] + "对怪物造成了" + power[weaponType] + "点伤害!"); attack(new Random().nextInt(3),monster); }else System.out.println("怪物被击毙"); } } class Monster { public int blood; } }
------解决方案--------------------
public class Test3 { public static void main(String[] args){ new Test3().run(); } public void run(){ Hero hero = new Hero(1); Monster monster = new Monster(1000); while(!monster.die()){ hero.attack(monster); } System.out.print("怪物被击毙!"); } } class Hero{ int weapon = 0; int heat = 0 ; double r = 0; String[] weapons = {"木剑","铁剑","魔剑"}; int[] heats = { 20 , 50 , 100 }; public Hero(int weapon){ this.weapon = weapon; } public void attack(Monster m){ r = Math.random(); heat = r<0.5 ? heats[weapon] : (heats[weapon]*2) ; m.blood = m.blood - heat; if(r>0.5){ System.out.print("暴击!"); } System.out.println("玩家使用"+weapons[weapon]+"对怪物造成了"+heat+"点伤害!"); } } class Monster{ int blood = 0; public Monster(int blood){ this.blood = blood; } public boolean die(){ if(blood<=0){ return true; } return false; } }