如何修改一个switch语句,它实质上是创建空轮?



我试图制作一个四人攻击游戏,随机选择攻击者并打印出获胜者,但在我的输出中,我一直得到空轮。我相信这是由于我的开关语句,但我不确定如何获得相同的结果减去这些基本上是空的回合。我已经创建了一个字符数组,将其放入随机生成的switch语句中,但是case中的if语句并没有像我想象的那样被跳过。我也试过从数组中删除玩家,因为他们死了,但这不起作用,并导致错误和异常消息。

import project3.characters.Plumber;
import project3.characters.Vampire;
import project3.characters.Werewolf;
import java.util.ArrayList;
import java.util.Random;
public class Game {
public static void main(String[] args) {
GameCharacter player1 = new Plumber("Mario", 10, 20);
GameCharacter player2 = new Fairy("Tinker Bell", 10, 20);
GameCharacter player3 = new Vampire("Edward", 10, 20);
GameCharacter player4 = new Werewolf("Derek", 10, 20);
int round = 0;
ArrayList<GameCharacter> player = new ArrayList<GameCharacter>. 
();
player.add(player1);
player.add(player2);
player.add(player3);
player.add(player4);
while (player1.isAlive() || player2.isAlive() || 
player3.isAlive() || player4.isAlive()) {
System.out.println("Round " + (round + 1) + ": ");
System.out.print(" " + player1 + " ");
System.out.println(" " + player2);
System.out.print(" " + player3 + " ");
System.out.println(" " + player4 + " ");
System.out.println();
// Only two players can battle
// if (round % 2 == 0) {
// player1.hit(player2.attack());
// } else
// player2.hit(player1.attack())
// All players randomly attack each other
Random rand = new Random();
int turn = rand.nextInt(12);
switch (turn) {
case 0:
if (player1.isAlive() && player2.isAlive())
player1.hit(player2.attack());
break;
case 1:
if (player1.isAlive() && player3.isAlive())
player1.hit(player3.attack());
break;
case 2:
if (player1.isAlive() && player4.isAlive())
player1.hit(player4.attack());
break;
case 3:
if (player1.isAlive() && player2.isAlive())
player2.hit(player1.attack());
break;
case 4:
if (player3.isAlive() && player2.isAlive())
player2.hit(player3.attack());
break;
case 5:
if (player4.isAlive() && player2.isAlive())
player2.hit(player4.attack());
break;
case 6:
if (player1.isAlive() && player3.isAlive())
player3.hit(player1.attack());
break;
case 7:
if (player3.isAlive() && player2.isAlive())
player3.hit(player2.attack());
break;
case 8:
if (player3.isAlive() && player4.isAlive())
player3.hit(player4.attack());
break;
case 9:
if (player1.isAlive() && player4.isAlive())
player4.hit(player1.attack());
break;
case 10:
if (player4.isAlive() && player2.isAlive())
player4.hit(player2.attack());
break;
case 11:
if (player3.isAlive() && player4.isAlive())
player4.hit(player3.attack());
break;
}
if (!player1.isAlive()) {
System.out.println(player1.getName() + " is dead!");
// player.remove(0);
// player1 = null
}
if (!player2.isAlive()) {
System.out.println(player2.getName() + " is dead!");
// player.remove(1);
// player2 = null
}
if (!player3.isAlive()) {
System.out.println(player3.getName() + " is dead!");
// player.remove(2);
// player3 = null
}
if (!player4.isAlive()) {
System.out.println(player4.getName() + " is dead!");
// player.remove(3);
// player4 = null
}
if (!player4.isAlive() && !player2.isAlive() && 
!player3.isAlive()) {
System.out.println(player1.getName() + " is the 
winner!");
break;
}
if (!player4.isAlive() && !player1.isAlive() && 
!player3.isAlive()) {
System.out.println(player2.getName() + " is the 
winner!");
break;
}
if (!player4.isAlive() && !player2.isAlive() && 
!player1.isAlive()) {
System.out.println(player3.getName() + " is the 
winner!");
break;
}
if (!player1.isAlive() && !player2.isAlive() && 
!player3.isAlive()) {
System.out.println(player4.getName() + " is the 
winner!");
break;
}


round++;
System.out.println();
}
}
}

一种方法是在战斗发生时保持一个标志。这样的:

boolean didFightHappen = true;
while (any player alive) {
//anounce round only if didFightHappen = true;
// round 1 etc

Random rand = new Random();
int turn = rand.nextInt(12);
didFightHappen = false;  // make flag false
switch (turn) {
case 0:
//if fight happens then set
// didFightHappen = true;
break;
}
// at end of the switch
if(didFightHappen == false) {
continue; // this continues the while loop to next iteration without executing rest of the loop code
}

}

这是你游戏的小版本:p

List<Integer> l = new ArrayList<Integer>();
l.add(1); l.add(2); l.add(3); l.add(4);

Collections.shuffle(l);
while(l.size()>1) {
int a = l.remove(0); 
int b = l.get(0);
System.out.println("Player " + b + " attacked player " + a );
System.out.println(a + " died!");
}
System.out.println("Player " + l.get(0) + " is the winner!");

输出:

Player 3 attacked player 4
4 died!
Player 1 attacked player 3
3 died!
Player 2 attacked player 1
1 died!
Playe 2 is the winner!

相关内容

  • 没有找到相关文章

最新更新