Java 游戏中的无限循环
我的任何类中都没有错误。我需要关于我的主类的语义的帮助。我运行这个应用程序,可以在没有任何东西再次出现之前输入任何我想要的玩家。它也不会退出。我的循环错误在哪里Java 游戏中的无限循环,java,loops,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[])
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();
}
}
}
package-Final;
//威廉·帕特森
//第一学期期末考试
导入java.util.*;
公共课程
{
专用静态扫描仪;
公共静态void main(字符串参数[])
{
字符串[]秩={“二”、“三”、“四”、“五”、“六”、“七”、“八”,
“九”、“十”、“杰克”、“女王”、“国王”、“王牌”};
字符串[]套装={“红心”、“钻石”、“黑桃”、“梅花”};
扫描=新扫描仪(System.in);
字符串something=“yes”,something2=“yes”,success=“yes”;//用于while循环
字符串turn=“一”;
字符串temp2,temp3;//用于正在搜索的播放器
卡片[]卡片组=新卡片[52];//卡片组数组
int playercount=0;
玩家一=新玩家(“临时”);
玩家二=新玩家(“临时”);
玩家三=新玩家(“临时”);
玩家四=新玩家(“临时”);
//欢迎光临
System.out.println(“欢迎来到我的节目:钓鱼!”);
System.out.println(“在玩游戏时,不要忘记玩家的名字!”);
println(“同样,这个版本的Go Fish!将在每个回合结束时获得一组四个类似的等级。”);
System.out.println();
//开始比赛
而(something.equalsIgnoreCase(“yes”))
{
//准备比赛
卡牌(牌组、军衔、套装);
deck=Card.getDeck();
洗牌(牌组);
while(something2.equalsIgnoreCase(“yes”)| | playercount<2)//将玩家添加到游戏中
{
System.out.println(“其他玩家愿意加入吗?”);
something2=scan.nextLine();
System.out.println();
如果(某物2.相等信号情况(“是”))
{
如果(播放计数3)
转动=“四”;
否则返回“一”;
success=“否”;
某物=三;
如果(某物等信号情况(“否”))
System.out.println(三个.getName()+“won!”);
}
}
//检查播放器4
if(temp2.equalsIgnoreCase(four.getName()))
{
if(four.searchHand(four.getHand(),temp3)!=null)
{
while(four.searchHand(four.getHand(),temp3)!=null)
{
3.addCard(4.searchHand(4.getHand(),temp3));
4.subCard(4.searchHand(4.getHand(),temp3));
三个子集(三个.getHand(),temp3);
}
}
else if(four.searchHand(four.getHand(),temp3)=null)
{
System.out.println();
System.out.println(“钓鱼!”);
System.out.println(三个.getName()+“画了一张卡”);
System.out.println();
三、加牌(牌、抽牌(牌));
三个子集(三个.getHand(),temp3);
deck=Card.getDeck();
如果(播放计数>3)
转动=“四”;
否则返回“一”;
success=“否”;
某物=三;
如果(某物等信号情况(“否”))
System.out.println(三个.getName()+“won!”);
}
}
//检查播放器2
if(temp2.equalsIgnoreCase(two.getName()))
{
if(two.searchHand(two.getHand(),temp3)!=null)
{
while(two.searchHand(two.getHand(),temp3)!=null)
{
3.addCard(2.searchHand(2.getHand(),temp3));
subCard(two.searchHand(two.getHand(),temp3));
三个子集(三个.getHand(),temp3);
}
}
else if(two.searchHand(two.getHand(),temp3)=null)
{
System.out.println();
System.out.println(“钓鱼!”);
System.out.println(三个.getName()+“画了一张卡”);
System.out.println();
三、加牌(牌、抽牌(牌));
三个子集(三个.getHand(),temp3);
deck=Card.getDeck();
如果(播放计数>3)
转动=“四”;
否则返回“一”;
success=“否”;
某物=三;
如果(某物等信号情况(“否”))
System.out.println(三个.getName()+“won!”);
}
}
}
}
//玩家4的轮到代码在这里,但必须缩写代码来发布这个问题
}
//重播
System.out.println(“您想再次播放吗?”);
something=scan.nextLine();
}
}
}
您的代码结构只是在尖叫着寻找bug。让每个玩家作为一个命名变量,而不是数组players[4]
,显然是不可理解的。
//Take turns
while (something.equalsIgnoreCase("yes"));