gpt4 book ai didi

java - 帮助将数组更改为 Java 中的单个变量

转载 作者:行者123 更新时间:2023-12-01 06:58:30 25 4
gpt4 key购买 nike

请帮忙。

我想添加这些选项,其中好人并购买 bat ,斧头或剑来对抗怪物。

我如何创建这 3 个数组,然后从中获取单个变量。那么用这些变量来影响游戏的结果呢?

  1. bat 最小伤害 = 2 最大伤害 = 4 成本 = 3
  2. 斧头最小伤害 = 4 最大伤害 = 6 成本 = 6
  3. 剑最小伤害 = 6 最大伤害 = 8 成本 = 10
  4. 取消
public static void main(String[] args)
throws IOException
{



int count = 1;

// start both with 1 point
int goodTotal = 50;
int monTotal = 50;
int moneyAmt = 10;

// input switch statement




loop: while (goodTotal > 0 && monTotal > 0) {


System.out.print("Type a letter: ");
System.out.println("\n");
System.out.print("F: Go out and Fight ");
System.out.println("\n");
System.out.print("R: Rest ");
System.out.println("\n");
System.out.print("C: Check Stats ");
System.out.println("\n");
System.out.print("Q: Quit ");
char input = (char) System.in.read();


System.out.println("You typed: " + (char)input);

switch(input) {
case 'f': System.out.println("Continue the game"); continue;

case 'r': System.out.println("Players should rest"); break loop;

case 'c': System.out.println("Checking the status of the game");
System.out.print("Goodman has " + goodTotal + " points and Monster has " + monTotal + " points");
System.out.println("\n"); break loop;

case 'q': System.out.println("Game over");
System.exit(input); break loop;

default: System.out.println("Invalid selection");break;
}


// Set value of minimum and maximum damage
int minDmg = 2;
int maxDmg = 15;

// Get random number;
int damage = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();
int damage2 = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();

// remove value of damage from started value to get total value remaining
goodTotal = goodTotal - damage;
monTotal = monTotal - damage2;


// print message if still in the game
if(goodTotal > 0){
System.out.println("Goodman has " + goodTotal + " points left. Not bad, Man! ");
}

// if Goodman survives round 2 print a message of encouragement
if (goodTotal > 0 && count > 1 && count <= 2){
System.out.print("This is encouraging. Goodman has lasted past roundhh " +count+ ". ");

// print new message if Goodman passes round 3
} else if (goodTotal > 0 && count == 3){
System.out.print("Goodman is as strong as Samson. He has lasted round " +count+ " and still looks strong.");
System.out.print(" 10 hit points has been added to your total");
}


if(monTotal > 0){
System.out.println("Wait, Monster has a total of " + monTotal + " points and is still in the game");
}


// exit if have less than 0 point, and print game over. Congratulate the winner
if(goodTotal < 0){
System.out.println("Goodman you are out of the game");
System.out.println("The monster will take over the village. This is sad");
System.out.println("Game Over!");
} else if(monTotal < 0){
System.out.println("Goodman has been victorious");
System.out.println("The monster is dead. The people live!!!!");
System.out.println("Game Over!");
}
System.out.println("This is the end of round " + count +" ");
System.out.println("\n");
count = count + 1;

}

}

最佳答案

Java 是一种面向对象的语言。因此,您可以对具有三个属性(minDamagemaxDamagecost)的类 Weapon 进行建模。然后创建该类的实例

Weapon axe = new Weapon(2,4,3);

稍后在代码中涉及伤害计算时,您可以使用 axe.getMinDamage()axe.getMaxDamage() 来设置随机函数的限制.

关于java - 帮助将数组更改为 Java 中的单个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5143866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com