【发布时间】:2019-11-29 08:09:24
【问题描述】:
我才刚开始编程 2 个月,所以我为我写得不好的代码道歉。如果您能给我关于如何改进代码的提示和建议,我将不胜感激。
我创建了一个创建英雄的程序。(在本例中,我使用了 dota 英雄) 程序首先询问英雄名称、生命值、法力值、攻击速度和攻击伤害。 在我创建了一个方法来询问用户它是什么类型的英雄之后。 选择是 1. 力量 2. 敏捷 3. 智力。每次选择都会设置英雄每级的攻击伤害、每级的攻击速度、每级的生命值和每级的mp。 在此之后,程序将要求用户选择一个级别(最高级别仅为 25) 选择级别后,程序将在升级后显示新属性。例如,如果用户选择 25,那么 Healthpool = healthpool + (hp per level * selected level) 等等。 在此之后程序将显示一个选择的敌人,在这个例子中是塔和肉山。 在用户选择一个敌人后,程序将显示一个选项,例如 1. 攻击 2. 结束。 我想要发生的是当用户选择攻击时,我希望程序在攻击后显示塔的当前 hp,然后将返回到如何操作的选项。 我真的不知道我应该做什么循环。
import java.util.Scanner;
class Hero {
Scanner sc = new Scanner(System.in);
String name, heroType, heroOpponent;
double baseHp, baseMp, baseAs, baseAd, asplvl, adplvl, hplvl, mplvl, tower;
int lvl, type, opponent, option;
void UserInput() {
System.out.print("Please enter hero name: ");
name = sc.nextLine();
System.out.print("Please enter your base Health Pool: ");
baseHp = sc.nextDouble();
System.out.print("Please enter your base Mana Pool: ");
baseMp = sc.nextDouble();
System.out.print("Please enter your base Attack Speed: ");
baseAs = sc.nextDouble();
System.out.print("Please enter your base Attack Damage: ");
baseAd = sc.nextDouble();
}
void TypeMethod() {
do {
System.out.println("Please select your hero type");
System.out.println();
System.out.println("1. Strength");
System.out.println("2. Agility");
System.out.println("3. Intelligence");
type = sc.nextInt();
switch (type) {
case 1:
heroType = ("Strength");
asplvl = 2.5;
adplvl = 5;
hplvl = 20;
mplvl = 12;
break;
case 2:
heroType = ("Agility");
asplvl = 7;
adplvl = 5;
hplvl = 12;
mplvl = 12;
break;
case 3:
heroType = ("Intelligence");
asplvl = 2.5;
adplvl = 5;
hplvl = 12;
mplvl = 20;
break;
default:
System.out.println("Selection Error! Try again!");
}
} while (type > 3 && type < 1);
}
void DisplayUserInput() {
System.out.println("Hero name: " + name);
System.out.println("Hero type: " + heroType);
System.out.println("Base Health Pool: " + baseHp);
System.out.println("Base Mana Pool: " + baseMp);
System.out.println("Base Attack Speed: " + baseAs);
System.out.println("Base Attack Damage: " + baseAd);
}
int HeroLevel() {
do {
System.out.println("Choose your level! Maximum level is 25");
lvl = sc.nextInt();
return lvl;
} while (lvl > 25 | lvl < 0);
}
double MpAfterLevel(int i) {
return baseMp = baseMp + (mplvl * (double) i);
}
double HpAfterLevel(int i) {
return baseHp + (hplvl * (double) i);
}
double AsAfterLevel(int i) {
return baseAs = baseAs + (baseAs * (double) i);
}
double AdAfterLevel(int i) {
return baseAd = baseAd + (baseAd * (double) i);
}
void DisplayHeroAfterLevel() {
System.out.println("Hero name : " + name);
System.out.println("Hero type : " + heroType);
System.out.println("Health Pool after at level " + lvl + " " + baseHp);
System.out.println("Mana Pool after at level " + lvl + " " + baseMp);
System.out.println("Attack Damage after at level " + lvl + " " + baseAd);
System.out.println("Attack Speed after at level " + lvl + " " + baseAs);
System.out.println();
}
void WhotoAttack() {
System.out.println("Please select an opponent");
System.out.println("1. Tower");
System.out.println("2. Roshan");
opponent = sc.nextInt();
switch (opponent) {
case 1:
do {
tower = 5000;
System.out.println("tower has " + tower + " Health points");
System.out.println("Please take action!");
System.out.println("1. Attack ");
System.out.println("2. End");
option = sc.nextInt();
switch (option) {
case 1:
case 2:
}
} while (tower != 0);
}
}
}
public class DotaHeroV1 {
public static void main(String args[]) {
Hero h1 = new Hero();
int i;
h1.UserInput();
h1.TypeMethod();
h1.DisplayUserInput();
i = h1.HeroLevel();
h1.HpAfterLevel(i);
h1.MpAfterLevel(i);
h1.AsAfterLevel(i);
h1.AdAfterLevel(i);
h1.DisplayHeroAfterLevel();
h1.WhotoAttack();
}
}
【问题讨论】:
-
我建议在 Google 或 DuckDuckGo 中搜索“java 循环”并阅读不同的类型。然后选择最适合您的。
-
我想你可能想尝试一下 - while 循环你可以在这里阅读:tutorialspoint.com/cprogramming/c_do_while_loop#。所以在 do 部分你会问用户他想做什么,而 while 部分会检查敌人是否有任何 hp 剩余
-
阅读如何构建游戏以在主事件循环中运行:gameprogrammingpatterns.com/game-loop.html
标签: java for-loop switch-statement