2016-03-27 1 views
-4

私は国に軍隊を置くことを要求するゲームに取り組んできました。軍隊とそのすべてを置くことはできますが、なんらかの理由で私はswitch文から抜け出せません。私が壊れようとすると、コード内のランダムな行にジャンプし、私は奇妙な出力に終わってしまいます。これは意味がなく、まだ関数/ 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 
+1

にフォールスルーで読むことができますどのようにあなたは何のbreak文を持っていない場合は、switch文から破ることを期待しますか? – Eran

+0

'break'文はどうですか? – kristu

+0

申し訳ありません私はそれを働かせようとしているときにそれを打破するために関数を使用しようとしました、私はそれが今どこにあるべきかに更新しました。 – RJBo

答えて

1

あなたはswitch文から抜け出すにしたい場合は、あなたがbreak文を追加する必要があります。それ以外の場合は、breakステートメントが見つかるか、スイッチのすべての行が実行されるまで、後続のcase文を実行し続けます。これは、Javaではフォールスルーと呼ばれています。 Oracleのサイトから

例:あなたはそれが唯一の8月を印刷したい場合は上記のコード

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月の場合にはbreak文を追加する必要がDecembet 。あなたは、任意の標準的なJavaの本

Break statement in Java switch

Oracle tutorial - switch statement

関連する問題