2016-03-27 15 views
-4

Ülkelere ordu yerleştirme gerektiren bir oyun üzerinde çalışıyorum. Orduları ve herşeyi yerleştirmek yerine, ama nedense anahtar deyimden çıkamıyorum. Kırmaya çalıştığımda, kodda rastgele satırlara atlar ve ben hiç bir anlam ifade etmeyen ve hala işlev/anahtar deyiminden çıkmayan garip bir çıkışla bitiririm. Java için göreceli olarak yeniyim, bu yüzden anahtar ifadelerinin nasıl çalıştığı konusunda eksik olan bir şey var mı? Sadece son davadan çıkmak istiyorum. Çok teşekkürler.Anahtarlama statüsünden çıkarılamıyor

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; 
} 

Çıktı:

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

düşüş oranı üzerinde okuyabilir nasıl hayır mola ifadeleri varsa switch ifadelerinde kırmak bekliyorsunuz? – Eran

+0

'break' deyiminden ne haber? – kristu

+0

Üzgünüm Çalışmaya çalışırken onu kırmak için bir işlev kullanmayı denedim, ara vermenin şimdi nerede olması gerektiği konusunda güncelledim. – RJBo

cevap

1

Eğer switch ifadesinin kurtulmaya istiyorsanız, break ifadesini eklemeniz gerekir. Else, break deyimi bulana kadar ya da anahtardaki tüm satırları çalıştırana kadar sonraki durum bildirimlerini çalıştırmaya devam edecektir. Buna Java'da düşüş denir. Oracle sitesinden

Örnek: sadece Ağustos yazdırmak isterseniz kod Üstü

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 

yazdırılır, değil sonra Ağustos ayı durumunda break ifadesini eklemeniz gerekir Decembet . Herhangi standart Java kitabında

Break statement in Java switch

Oracle tutorial - switch statement

İlgili konular