私は戦闘シーケンスを作ろうとしていますが、実際の戦闘を繰り返す必要があります。しかし、HPとenemyHPは、ランダムに生成された数値から最初のループの後に合計を引くようには見えません。ループが適用された後にランダムに生成された数値は機能しません
これが意味をなさない場合は申し訳ありません。私は物事を説明するのは本当に良いではないよ...
ここではコードです:
import java.util.Random;
import java.util.Scanner;
public class Hayyan {
public int hp = 100;
public int choice = 0;
public static void main(String[] args) {
combatHayyan bob = new combatHayyan();
Hayyan junl = new Hayyan();
while(junl.choice < 10){
System.out.println("Choose a weapon to attack with, type sword for a sword, or axe for an axe ");
bob.attack();
bob.defend();
System.out.println();
if(junl.hp < 0 || bob.enemyHP < 0){
junl.choice = 10;
}
}
}
}
class combatHayyan {
public int enemyHP = 50;
public int yourTotal;
public void attack(){
weapons weapon = new weapons();
Scanner bob = new Scanner(System.in);
switch(bob.nextLine()){
case "sword":
weapon.sword();
System.out.println("Enemy now has " + weapon.total + "HP left!");
break;
case "axe":
weapon.axe();
System.out.println("Enemy now has " + weapon.total + "HP left!");
break;
}
}
public void defend(){
Hayyan lost = new Hayyan();
Random bob = new Random();
int randomness = bob.nextInt(11) + 10;
yourTotal = lost.hp - randomness;
System.out.println("You now have " + yourTotal + "HP left!");
}
}
class weapons {
public int total;
public void sword(){
int bob = 5 + (int)(Math.random() * ((10 - 5) + 1));
combatHayyan llama = new combatHayyan();
total = llama.enemyHP - bob;
}
public void axe(){
int randomGenerate = 5 + (int)(Math.random() * ((10 - 5) + 1));
combatHayyan llama = new combatHayyan();
total = llama.enemyHP - randomGenerate;
}
}
ありがとう、私はそれを試してみましょう! –
防衛のために働いていましたが、攻撃のためにそれはまだ再計算を続けています –
あなたの変数は毎回再作成される武器で宣言されています..私はあなたのコードを屈折させる方法を示すために私の答えを編集しました。 – alexbt