Java 我的程序执行一个额外的循环,如何修复?
我正在编写一个高一低的纸牌游戏。目的是猜测第二张牌是比第一张牌大还是小。除了一个问题外,该程序运行良好。在游戏中,玩家以5个积分开始。每猜一次,他就获得一个学分,最多10个,如果他没有猜到,他就失去1个学分,降到0。 在0点或10点,他应该得到一个你赢了!否则你就输了!消息,他确实这样做了,但问题是程序在之后还是多画了一张 我首先使用了while循环,然后尝试了do while循环,但问题是相同的 以下是主要方法:Java 我的程序执行一个额外的循环,如何修复?,java,loops,while-loop,do-while,Java,Loops,While Loop,Do While,我正在编写一个高一低的纸牌游戏。目的是猜测第二张牌是比第一张牌大还是小。除了一个问题外,该程序运行良好。在游戏中,玩家以5个积分开始。每猜一次,他就获得一个学分,最多10个,如果他没有猜到,他就失去1个学分,降到0。 在0点或10点,他应该得到一个你赢了!否则你就输了!消息,他确实这样做了,但问题是程序在之后还是多画了一张 我首先使用了while循环,然后尝试了do while循环,但问题是相同的 以下是主要方法: public static void main(String[] args)
public static void main(String[] args)
{
HigherLower h = new HigherLower();
boolean higher; // higher = true; lower = false
Draw draw = new Draw(); Deck deck = new Deck();
Card card1; Card card2;
int credits = 5;
boolean playing = true;
while(playing)
{
if(credits == 0)
{
System.out.println("\nYou lose !");
playing = false;
}
else if(credits == 10)
{
System.out.println("\nYou win !");
playing = false;
}
System.out.println("\n\n\n\n NEW DRAW : ");
card1 = draw.drawCard(deck);
System.out.print(card1.getName());
boolean choice = h.getRightInput();
card2 = draw.drawCard(deck);
if((card2.getValue() > card1.getValue() && choice == true)
|| (card2.getValue() < card1.getValue() && choice == false))
{
credits++;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed right ! "
+ "Now you have %d credits", credits);
}
else
{
credits--;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed wrong ! "
+ "Now you have %d credits", credits);
}
}
}
玩家以5个积分开始游戏,这意味着游戏不会在开始的那一刻结束。您应该翻转循环的各个部分-首先进行绘图,然后检查游戏是否结束:
while (playing) {
// Code to do another drawing
if (credits == 0) {
System.out.println("\nYou lose !");
playing = false;
} else if (credits == 10) {
System.out.println("\nYou win !");
playing = false;
}
}
玩家以5个积分开始游戏,这意味着游戏不会在开始的那一刻结束。您应该翻转循环的各个部分-首先进行绘图,然后检查游戏是否结束:
while (playing) {
// Code to do another drawing
if (credits == 0) {
System.out.println("\nYou lose !");
playing = false;
} else if (credits == 10) {
System.out.println("\nYou win !");
playing = false;
}
}
把你赢了和你输了的球移动到圈底
while(playing)
{
System.out.println("\n\n\n\n NEW DRAW : ");
card1 = draw.drawCard(deck);
System.out.print(card1.getName());
boolean choice = h.getRightInput();
card2 = draw.drawCard(deck);
if((card2.getValue() > card1.getValue() && choice == true)
|| (card2.getValue() < card1.getValue() && choice == false))
{
credits++;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed right ! "
+ "Now you have %d credits", credits);
}
else
{
credits--;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed wrong ! "
+ "Now you have %d credits", credits);
}
if(credits == 0)
{
System.out.println("\nYou lose !");
playing = false;
}
else if(credits == 10)
{
System.out.println("\nYou win !");
playing = false;
}
}
同时(播放)
{
System.out.println(“\n\n\n\n新绘图:”);
card1=提款卡(卡片组);
System.out.print(card1.getName());
布尔选择=h.getRightInput();
卡片2=抽卡(卡片组);
if((card2.getValue()>card1.getValue()&&choice==true)
||(card2.getValue()
将赢家和输家移动到循环的底部
while(playing)
{
System.out.println("\n\n\n\n NEW DRAW : ");
card1 = draw.drawCard(deck);
System.out.print(card1.getName());
boolean choice = h.getRightInput();
card2 = draw.drawCard(deck);
if((card2.getValue() > card1.getValue() && choice == true)
|| (card2.getValue() < card1.getValue() && choice == false))
{
credits++;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed right ! "
+ "Now you have %d credits", credits);
}
else
{
credits--;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed wrong ! "
+ "Now you have %d credits", credits);
}
if(credits == 0)
{
System.out.println("\nYou lose !");
playing = false;
}
else if(credits == 10)
{
System.out.println("\nYou win !");
playing = false;
}
}
同时(播放)
{
System.out.println(“\n\n\n\n新绘图:”);
card1=提款卡(卡片组);
System.out.print(card1.getName());
布尔选择=h.getRightInput();
卡片2=抽卡(卡片组);
if((card2.getValue()>card1.getValue()&&choice==true)
||(card2.getValue()
如果条件满足,你可以考虑中断循环。移动条件将不会起任何作用,它将打印额外的数据
如果条件满足,你可以考虑中断循环。移动条件不会产生任何效果,但它仍会打印额外数据。请尝试以下代码:
if(credits == 0) {
System.out.println("\nYou lose !");
playing = false;
break;
}
else if(credits == 10) {
System.out.println("\nYou win !");
playing = false;
break;
}
请尝试以下代码:
if(credits == 0) {
System.out.println("\nYou lose !");
playing = false;
break;
}
else if(credits == 10) {
System.out.println("\nYou win !");
playing = false;
break;
}
当然,由于没有返回while循环,所以“Draw”指令出现在“win”或“lose”之后。我建议您使用“继续”或“中断”停止处理下一个指令:
while(playing)
{
if(credits == 0)
{
System.out.println("\nYou lose !");
playing = false;continue;
}
else if(credits == 10)
{
System.out.println("\nYou win !");
playing = false;continue;
}
当然,由于没有返回while循环,所以“Draw”指令出现在“win”或“lose”之后。我建议您使用“继续”或“中断”停止处理下一个指令:
while(playing)
{
if(credits == 0)
{
System.out.println("\nYou lose !");
playing = false;continue;
}
else if(credits == 10)
{
System.out.println("\nYou win !");
playing = false;continue;
}
你说得对。这有帮助。非常感谢你!你说得对。这有帮助。非常感谢你!