我一直致力于游戏,这需要在国家安置军队。放置军队和所有可行的,但由于某种原因,我无法摆脱开关声明。当我试图打破时,它跳到代码中的随机行,我最终得到一个奇怪的输出,这没有任何意义,仍然没有退出函数/ switch语句。我对Java比较新,所以关于switch语句如何工作,我有什么遗漏?我只想突破最后一个案例。非常感谢。
public void terrPlacement(Card[] deck, gpp gp, UI ui, Player player1, Player player2, Player neutral1, Player neutral2, Player neutral3, Player neutral4, Board board, ArrayList p1Terr, ArrayList p2Terr, ArrayList n1Terr, ArrayList n2Terr, ArrayList n3Terr, ArrayList n4Terr) {
String locationEntered;
int playerId =0, countryId=0;
switch(roll) {
case "player1":
while (player1.armyCount>0) {
ui.displayString("\nEnter the name of the territory you wish to place units on\n");
locationEntered=ui.getCommand();
ui.displayString("> " + locationEntered);
if (p1Terr.contains(locationEntered)) { // Compares what was entered to array list of country names
for (int r=0; r<42; r++) { // then gets countryId of that country
if (locationEntered.equals(deck[r].cardName())){
countryId=deck[r].countryId;
}
}
board.addUnits(countryId, playerId, 3); // uses countryId to place units on country entered above
player1.armyCount-=3;
ui.displayString("Your new army count is: " + player1.getArmyCount()); // if army count isn't 0 it continues in loop of adding more units
ui.displayMap();
terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr);
}
else if (!p2Terr.contains(locationEntered)) {
ui.displayString("You either do not own this territory or " // if territory name isnt one they own it throws an error
+ "you spelt the territory name incorrectly, please try again");
}
}
if (player2.armyCount!=0) { // when army count is 0 it moves on to player 2, but only if player two hasn't already gone
roll="player2";
ui.displayString("\nPlayer 2 place your army units");
terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr);
}
else if (player2.armyCount==0){ // if player two has already gone it moves onto neutral 1
roll="neutral1";
ui.displayString("\nPlease place the armies units for neutral 1 now");
terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr);
}
case "player2":
// same principle as above case
while (player2.armyCount>0) {
ui.displayString("\nEnter the name of the territory you wish to place units on");
locationEntered=ui.getCommand();
ui.displayString("> " + locationEntered);
playerId=1;
countryId=0;
roll="player2";
if (p2Terr.contains(locationEntered)) {
for (int r=0; r<42; r++) {
if (locationEntered.equals(deck[r].cardName())){
countryId=deck[r].countryId;
}
}
board.addUnits(countryId, playerId, 3);
player2.armyCount-=3;
ui.displayString("Your new army count is: " + player2.getArmyCount());
ui.displayMap();
}
else if (!p2Terr.contains(locationEntered)) {
ui.displayString("You either do not own this territory or "
+ "you spelt the territory name incorrectly, please try again");
}
}
ui.displayString("You're out of armies!");
if (player1.armyCount!=0) {
roll="player1";
ui.displayString("\nPlayer 1 place your army units");
terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr);
}
case "neutral1":
while (neutral1.armyCount>0) {
ui.displayString("\nEnter the name of the territory you wish to place a unit on\n");
locationEntered=ui.getCommand();
ui.displayString("> " + locationEntered);
playerId=2;
countryId=0;
if (n1Terr.contains(locationEntered)) {
for (int r=0; r<42; r++) {
if (locationEntered.equals(deck[r].cardName())){
countryId=deck[r].countryId;
}
}
board.addUnits(countryId, playerId, 1);
neutral1.armyCount--;
ui.displayString("Your new army count is: " + neutral1.getArmyCount());
ui.displayMap();
}
else if (!n1Terr.contains(locationEntered)) {
ui.displayString("You either do not own this territory or "
+ "you spelt the territory name incorrectly, please try again");
}
}
ui.displayString("\nNeutral 1 is out of armies");
case "neutral2":
while (neutral2.armyCount>0) {
ui.displayString("\nEnter the name of the territory you wish to place a unit on\n");
locationEntered=ui.getCommand();
ui.displayString("> " + locationEntered);
playerId=3;
countryId=0;
if (n2Terr.contains(locationEntered)) {
for (int r=0; r<42; r++) {
if (locationEntered.equals(deck[r].cardName())){
countryId=deck[r].countryId;
}
}
board.addUnits(countryId, playerId, 1);
neutral2.armyCount--;
ui.displayString("Your new army count is: " + neutral2.getArmyCount());
ui.displayMap();
}
else if (!n2Terr.contains(locationEntered)) {
ui.displayString("You either do not own this territory or "
+ "you spelt the territory name incorrectly, please try again");
}
}
ui.displayString("\nNeutral 2 is out of armies");
case "neutral3":
while (neutral3.armyCount>0) {
ui.displayString("\nEnter the name of the territory you wish to place a unit on\n");
locationEntered=ui.getCommand();
ui.displayString("> " + locationEntered);
playerId=4;
countryId=0;
if (n3Terr.contains(locationEntered)) {
for (int r=0; r<42; r++) {
if (locationEntered.equals(deck[r].cardName())){
countryId=deck[r].countryId;
}
}
board.addUnits(countryId, playerId, 1);
neutral3.armyCount--;
ui.displayString("Your new army count is: " + neutral3.getArmyCount());
ui.displayMap();
}
else if (!n3Terr.contains(locationEntered)) {
ui.displayString("You either do not own this territory or "
+ "you spelt the territory name incorrectly, please try again");
}
}
ui.displayString("\nNeutral 3 is out of armies");
}
break;
}
输出:
Welcome to Error 420's Risk!
Enter the name of Player 1
> Rob
Enter the name of Player 2
> Peter
Player 1's territory cards are:
Great Britain
W United States
Quebec
Ural
Middle East
Brazil
W Europe
Ontario
Central America
Player 2's territory cards are:
W Australia
Egypt
S Africa
Alaska
Japan
New Guinea
Afghanistan
NW Territory
Siam
Neutral 1's territory cards are:
N Europe
India
Scandinavia
Irkutsk
Madagascar
Ukraine
Neutral 2's territory cards are:
Greenland
Indonesia
Argentina
Mongolia
Siberia
Kamchatka
Neutral 3's territory cards are:
Peru
Alberta
Iceland
E Australia
E United States
E Africa
Neutral 4's territory cards are:
Yakutsk
Congo
S Europe
Venezuela
China
N Africa
All initial territory cards have been drawn.
Players will now place their armies...
Peter will reinforce their territories first, as they rolled a 11 and Rob only rolled a 9
Enter the name of the territory you wish to place units on
> Japan
Your new army count is: 0
You're out of armies!
Player 1 place your army units
Enter the name of the territory you wish to place units on
> Ural
Your new army count is: 0
You're out of armies!
Enter the name of the territory you wish to place a unit on
> India
Your new army count is: 0
Neutral 1 is out of armies
Enter the name of the territory you wish to place a unit on
> Siberia
Your new army count is: 0
Neutral 2 is out of armies
Enter the name of the territory you wish to place a unit on
> Peru
Your new army count is: 0
Neutral 3 is out of armies
p142
p242
You're out of armies!
Neutral 1 is out of armies
Neutral 2 is out of armies
Neutral 3 is out of armies
p142
p242
Neutral 1 is out of armies
Neutral 2 is out of armies
Neutral 3 is out of armies
p142
p242
p142
p242
Peter will play first as they rolled a 6 and Rob only rolled a 3
答案 0 :(得分:1)
如果要中断switch语句,则需要添加break语句。否则它将继续运行后续的case语句,直到找到break语句或在switch中运行所有行。这在Java中称为“直通”。
Oracle站点的示例:
int month = 8;
switch (month) {
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
break;
default: break;
}
以上代码将打印
August
September
October
November
December
如果您希望它仅在8月份打印,则需要在8月份添加break语句,而不是在Decembet之后添加。您可以阅读任何标准Java书籍中的翻译