我的程序做了一个额外的循环,如何解决?

时间:2016-05-05 16:14:46

标签: java loops while-loop do-while

我正在编写一个更低级别的纸牌游戏。目标是猜测第二张卡是否比第一张卡更大或更小。除了一个问题,该程序工作正常。在游戏中,玩家以5个学分开始。每次猜测他都会获得一个最多10个积分,或者如果他没有猜到他会失去1个积分,那么就会降到0。 在0或10,他应该得到你赢!或者你输了!消息,他做了,但问题是该程序事后还做了一次额外的抽奖。

我首先使用 while 循环,然后我尝试了 do while 循环,但问题是相同的。

这是主要方法:

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




         }


      }

以下是其他类,我将它们彼此相邻:

       public class Card 
        {
               String name;
               int value;

            Card(String name, int value)
            {
                this.name = name;
                this.value = value;
            }

            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public int getValue() {
                return value;
            }
            public void setValue(int value) {
                this.value = value;
            }
        }

public class Deck 
    {
        Card Joker = new Card("Joker", 1); 
        Card Two = new Card("2", 2);
        Card Three = new Card("3", 3);
        Card Four = new Card("4", 4);
        Card Five = new Card("5", 5);
        Card Six = new Card("6", 6);
        Card Seven = new Card("7", 7);
        Card Eight = new Card("8", 8);
        Card Nine = new Card("9", 9);
        Card Ten = new Card("10", 10);
        Card Jack = new Card("J", 11);
        Card Queen = new Card("Q", 12);
        Card King = new Card("K", 13);
        Card Ace = new Card("A", 14);

        Card[] deck = new Card[]{Joker, Two, Three, Four, Five,
                                 Six, Seven, Eight, Nine, Ten, 
                                 Jack, Queen, King, Ace};

    }


import java.util.Random;

public class Draw 
{

     Random random = new Random();

     public Card drawCard(Deck deck)
     {
         int maxNumber = deck.deck.length  ;

         int draw = random.nextInt(maxNumber);

         Card drawedCard = deck.deck[draw];

         return drawedCard;

     }


}

public class HigherLower 
{

     Scanner sc = new Scanner(System.in);

     public boolean getRightInput()
     {
         while(true)
         {
         System.out.println("\n (H)igher or (L)ower ? ");

         String s = sc.next();
         char c = s.charAt(0);

            if(c == 'H' || c == 'h')
            {
                return true;
            }
            else if(c == 'L' || c == 'l')
            {
                return false;
            }
            else
            {
                System.out.println("Give valid input ! H or L");
            }

         }

     }
}

5 个答案:

答案 0 :(得分:1)

玩家以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;
     }
}

答案 1 :(得分:0)

将You Win和You Lose移动到循环的底部

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




         }

答案 2 :(得分:0)

while(playing)
         {
             if(credits == 0)
             {
                 System.out.println("\nYou lose !");
                 playing = false;
                 break;
             }
             else if(credits == 10)
             {
                 System.out.println("\nYou win !");
                 playing = false;
                 break;
             }

如果符合条件,你可以考虑打破循环。移动条件无论如何都无法打印额外的数据。

答案 3 :(得分:0)

确保“获取”指令在“赢”或“输”后出现,因为你没有回到while循环。我建议你使用continue或break来停止处理下一个指令:

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

答案 4 :(得分:-1)

试试这段代码:

     if(credits == 0) {
                     System.out.println("\nYou lose !");
                     playing = false;
break;
                 }
                 else if(credits == 10) {
                     System.out.println("\nYou win !");
                     playing = false;
break;
                 }