在游戏的某个地方无限循环

时间:2013-12-19 04:26:54

标签: java loops

我的任何类都没有错误。我需要帮助我的主类的语义。我运行应用程序,能够通过输入我想要的任何球员,然后再没有任何东西出现。它也不会退出。我的循环错误在哪里?

package Final;

//William Patterson
//Semester 1 Final
import java.util.*;

public class Program
{
private static Scanner scan;

public static void main(String args[]) 
{
  String[] rank = {"two", "three", "four", "five", "six", "seven", "eight",
               "nine", "ten", "jack", "queen", "king", "ace"};
  String[] suit = {"hearts", "diamonds", "spades", "clubs"};
  scan = new Scanner(System.in);
  String something = "yes", something2 = "yes", success = "yes"; //Use with while loops
  String turn = "one";
  String temp2, temp3; //For player being searched
  Card[] deck = new Card[52]; //Deck array
  int playercount = 0;
  Player one = new Player("temp");
  Player two = new Player("temp");
  Player three = new Player("temp");
  Player four = new Player("temp");

  //Welcome
  System.out.println("Welcome to my program: Go Fish!");
  System.out.println("When playing, don't forget players' names!");
  System.out.println("Also, this version of Go Fish! will secure a set of four similar ranks at each turn's end.");
  System.out.println();

  //Start game
  while (something.equalsIgnoreCase("yes"))
  {
      //Prepare game
      Card.makeDeck(deck, rank, suit);
      deck = Card.getDeck();
      Card.shuffle(deck);

      while (something2.equalsIgnoreCase("yes") || playercount < 2) //Add players to game
      {
          System.out.println("Would a(nother) player like to join?");
          something2 = scan.nextLine();
          System.out.println();
          if (something2.equalsIgnoreCase("yes"))
          {
              if (playercount <= 4)
              {
                  if (playercount == 0)
                  {
                      System.out.println("What is your name: ");
                      Player one1 = new Player(scan.nextLine());
                      one = one1;
                      playercount++;
                      System.out.println();
                  }
                  else if (playercount == 1)
                  {
                      System.out.println("What is your name: ");
                      Player two2 = new Player(scan.nextLine());
                      two = two2;
                      playercount++;
                      System.out.println();
                  }
                  else if (playercount == 2)
                  {
                      System.out.println("What is your name: ");
                      Player three3 = new Player(scan.nextLine());
                      three = three3;
                      playercount++;
                      System.out.println();
                  }
                  else if (playercount == 3)
                  {
                      System.out.println("What is your name: ");
                      Player four4 = new Player(scan.nextLine());
                      four = four4;
                      playercount++;
                      System.out.println();
                  }
                  else {System.out.println("Only four players are allowed.");
                        something2 = "no";}
              }
          }
          else if (playercount < 2)
          {
              System.out.println("You need at least two players...");
              System.out.println();
          }
      }
      //Deal cards
      if (playercount == 2) 
      {
          for (int i = 1; i < 8; i++)
          {
              one.addCard(Card.draw(deck));
              deck = Card.getDeck();
              two.addCard(Card.draw(deck));
              deck = Card.getDeck();
          }
      }
      else if (playercount == 3) 
      {
          for (int i = 1; i < 8; i++) 
          {
              one.addCard(Card.draw(deck));
              deck = Card.getDeck();
              two.addCard(Card.draw(deck));
              deck = Card.getDeck();
              three.addCard(Card.draw(deck));
              deck = Card.getDeck();
          }
      }
      else
      {
          for (int i = 1; i < 6; i++)
          {
              one.addCard(Card.draw(deck));
              deck = Card.getDeck();
              two.addCard(Card.draw(deck));
              deck = Card.getDeck();
              three.addCard(Card.draw(deck));
              deck = Card.getDeck();
              four.addCard(Card.draw(deck));
              deck = Card.getDeck();
          }
      }
      //Take turns
      while (something.equalsIgnoreCase("yes"));
      {
          success = "yes";
          if (turn.equalsIgnoreCase("one") && something.equalsIgnoreCase("yes"))
              {
                  System.out.println("Player one: " + one.getName() + "'s turn!");
                  System.out.println();
                  //Output hand
                  System.out.println("You have: " + Card.toString(one.getHand()));
                  System.out.println();
                  //Ask what who to search
                  System.out.println("Whose hand would you like to check?");
                  System.out.println("(Enter a player's name.)");
                  temp2 = scan.nextLine(); //Set player name to temp2
                  System.out.println();
                  //Ask what to search
                  while (success.equalsIgnoreCase("yes"))
                  {
                      System.out.println("Remember, you have: " + Card.toString(one.getHand()));
                      System.out.println();
                      System.out.println("Would you like to search for a two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace?");
                      temp3 = scan.nextLine(); //Set desired rank to temp3
                      //Check player two
                      if (temp2.equalsIgnoreCase(two.getName()))
                      {
                          if (two.searchHand(two.getHand(), temp3) != null)
                          {
                              while (two.searchHand(two.getHand(), temp3) != null)
                              {
                                  one.addCard(two.searchHand(two.getHand(), temp3));
                                  two.subCard(two.searchHand(two.getHand(), temp3));
                                  one.subSets(one.getHand(), temp3);
                              }
                          }
                          else if (two.searchHand(two.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(one.getName() + " drew a card.");
                              System.out.println();
                              one.addCard(Card.draw(deck));
                              one.subSets(one.getHand(), temp3);
                              deck = Card.getDeck();
                              turn = "two";
                              success = "no";
                              something = one.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(one.getName() + " won!");
                          }
                      }
                      //Check player three
                      if (temp2.equalsIgnoreCase(three.getName()))
                      {
                          if (three.searchHand(three.getHand(), temp3) != null)
                          {
                              while (three.searchHand(three.getHand(), temp3) != null)
                              {
                                  one.addCard(three.searchHand(three.getHand(), temp3));
                                  three.subCard(three.searchHand(three.getHand(), temp3));
                                  one.subSets(one.getHand(), temp3);
                              }
                          }
                          else if (three.searchHand(three.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(one.getName() + " drew a card.");
                              System.out.println();
                              one.addCard(Card.draw(deck));
                              one.subSets(one.getHand(), temp3);
                              deck = Card.getDeck();
                              turn = "two";
                              success = "no";
                              something = one.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(one.getName() + " won!");
                          }
                      }
                      //Check player four
                      if (temp2.equalsIgnoreCase(four.getName()))
                      {
                          if (four.searchHand(four.getHand(), temp3) != null)
                          {
                              while (four.searchHand(four.getHand(), temp3) != null)
                              {
                                  one.addCard(four.searchHand(four.getHand(), temp3));
                                  four.subCard(four.searchHand(four.getHand(), temp3));
                                  one.subSets(one.getHand(), temp3);
                              }
                          }
                          else if (four.searchHand(four.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(one.getName() + " drew a card.");
                              System.out.println();
                              one.addCard(Card.draw(deck));
                              one.subSets(one.getHand(), temp3);
                              deck = Card.getDeck();
                              turn = "two";
                              success = "no";
                              something = one.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(one.getName() + " won!");
                          }
                      }
                  }
              }
          //Player two's turn
          if (turn.equalsIgnoreCase("two") && something.equalsIgnoreCase("yes"))
              {
                  System.out.println("Player two: " + two.getName() + "'s turn!");
                  System.out.println();
                  //Output hand
                  System.out.println("You have: " + Card.toString(two.getHand()));
                  System.out.println();
                  //Ask what who to search
                  System.out.println("Whose hand would you like to check?");
                  System.out.println("(Enter a player's name.)");
                  temp2 = scan.nextLine(); //Set player name to temp2
                  System.out.println();
                  //Ask what to search
                  while (success.equalsIgnoreCase("yes"))
                  {
                      System.out.println("Remember, you have: " + Card.toString(two.getHand()));
                      System.out.println();
                      System.out.println("Would you like to search for a two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace?");
                      temp3 = scan.nextLine(); //Set desired rank to temp3
                      //Check player one
                      if (temp2.equalsIgnoreCase(one.getName()))
                      {
                          if (one.searchHand(one.getHand(), temp3) != null)
                          {
                              while (one.searchHand(one.getHand(), temp3) != null)
                              {
                                  two.addCard(one.searchHand(one.getHand(), temp3));
                                  one.subCard(one.searchHand(one.getHand(), temp3));
                                  two.subSets(two.getHand(), temp3);
                              }
                          }
                          else if (two.searchHand(two.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(two.getName() + " drew a card.");
                              System.out.println();
                              two.addCard(Card.draw(deck));
                              two.subSets(two.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount >= 3)
                                  turn = "three";
                              else turn = "one";
                              success = "no";
                              something = two.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(two.getName() + " won!");
                          }
                      }
                      //Check player three
                      if (temp2.equalsIgnoreCase(three.getName()))
                      {
                          if (three.searchHand(three.getHand(), temp3) != null)
                          {
                              while (three.searchHand(three.getHand(), temp3) != null)
                              {
                                  two.addCard(three.searchHand(three.getHand(), temp3));
                                  three.subCard(three.searchHand(three.getHand(), temp3));
                                  two.subSets(two.getHand(), temp3);
                              }
                          }
                          else if (three.searchHand(three.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(two.getName() + " drew a card.");
                              System.out.println();
                              two.addCard(Card.draw(deck));
                              two.subSets(two.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount >= 3)
                                  turn = "three";
                              else turn = "one";
                              success = "no";
                              something = two.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(two.getName() + " won!");
                          }
                      }
                      //Check player four
                      if (temp2.equalsIgnoreCase(four.getName()))
                      {
                          if (four.searchHand(four.getHand(), temp3) != null)
                          {
                              while (four.searchHand(four.getHand(), temp3) != null)
                              {
                                  two.addCard(four.searchHand(four.getHand(), temp3));
                                  four.subCard(four.searchHand(four.getHand(), temp3));
                                  two.subSets(two.getHand(), temp3);
                              }
                          }
                          else if (four.searchHand(four.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(two.getName() + " drew a card.");
                              System.out.println();
                              two.addCard(Card.draw(deck));
                              two.subSets(two.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount >= 3)
                                  turn = "three";
                              else turn = "one";
                              success = "no";
                              something = two.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(two.getName() + " won!");
                          }
                      }
                  }
              }
          //Player three's turn
          if (turn.equalsIgnoreCase("three") && something.equalsIgnoreCase("yes"))
              {
                  System.out.println("Player three: " + three.getName() + "'s turn!");
                  System.out.println();
                  //Output hand
                  System.out.println("You have: " + Card.toString(two.getHand()));
                  System.out.println();
                  //Ask what who to search
                  System.out.println("Whose hand would you like to check?");
                  System.out.println("(Enter a player's name.)");
                  temp2 = scan.nextLine(); //Set player name to temp2
                  System.out.println();
                  //Ask what to search
                  while (success.equalsIgnoreCase("yes"))
                  {
                      System.out.println("Remember, you have: " + Card.toString(two.getHand()));
                      System.out.println();
                      System.out.println("Would you like to search for a two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace?");
                      temp3 = scan.nextLine(); //Set desired rank to temp3
                      //Check player one
                      if (temp2.equalsIgnoreCase(one.getName()))
                      {
                          if (one.searchHand(one.getHand(), temp3) != null)
                          {
                              while (one.searchHand(one.getHand(), temp3) != null)
                              {
                                  three.addCard(one.searchHand(one.getHand(), temp3));
                                  one.subCard(one.searchHand(one.getHand(), temp3));
                                  three.subSets(three.getHand(), temp3);
                              }
                          }
                          else if (one.searchHand(one.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(three.getName() + " drew a card.");
                              System.out.println();
                              three.addCard(Card.draw(deck));
                              three.subSets(three.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount > 3)
                                  turn = "four";
                              else turn = "one";
                              success = "no";
                              something = three.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(three.getName() + " won!");
                          }
                      }
                      //Check player four
                      if (temp2.equalsIgnoreCase(four.getName()))
                      {
                          if (four.searchHand(four.getHand(), temp3) != null)
                          {
                              while (four.searchHand(four.getHand(), temp3) != null)
                              {
                                  three.addCard(four.searchHand(four.getHand(), temp3));
                                  four.subCard(four.searchHand(four.getHand(), temp3));
                                  three.subSets(three.getHand(), temp3);
                              }
                          }
                          else if (four.searchHand(four.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(three.getName() + " drew a card.");
                              System.out.println();
                              three.addCard(Card.draw(deck));
                              three.subSets(three.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount > 3)
                                  turn = "four";
                              else turn = "one";
                              success = "no";
                              something = three.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(three.getName() + " won!");
                          }
                      }
                      //Check player two
                      if (temp2.equalsIgnoreCase(two.getName()))
                      {
                          if (two.searchHand(two.getHand(), temp3) != null)
                          {
                              while (two.searchHand(two.getHand(), temp3) != null)
                              {
                                  three.addCard(two.searchHand(two.getHand(), temp3));
                                  two.subCard(two.searchHand(two.getHand(), temp3));
                                  three.subSets(three.getHand(), temp3);
                              }
                          }
                          else if (two.searchHand(two.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(three.getName() + " drew a card.");
                              System.out.println();
                              three.addCard(Card.draw(deck));
                              three.subSets(three.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount > 3)
                                  turn = "four";
                              else turn = "one";
                              success = "no";
                              something = three.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(three.getName() + " won!");
                          }
                      }
                  }
              }
          //player 4's turn code here, but had to abbreviate code to post this question
      }
      //Replay
      System.out.println("Would you like to play again?");
      something = scan.nextLine();
  }
}

}

2 个答案:

答案 0 :(得分:2)

源中的这一行可能会导致它无限循环。

  //Take turns
  while (something.equalsIgnoreCase("yes"));

while循环结束时的分号是一个很大的 NO

答案 1 :(得分:1)

你的代码结构只是在为bug而尖叫。将每个玩家作为命名变量而不是数组players[4],坦率地说是不可理解的。因此,您最终会得到大量近似重复的代码,并有更多机会破解事物。

这可能比你想要在截止日期临近时更加激进;所以首先将你的代码简化为双人游戏 - 复制你的程序,然后无情地删除。很多ifs将消失,结构将变得更清晰。然后将它减少到骨架 - 只是循环的条件,内部没有代码。在这一点上,应该很明显发生了什么。