【问题标题】:My program does one extra loop, how to fix?我的程序做了一个额外的循环,如何解决?
【发布时间】:2016-05-05 16:14:46
【问题描述】:

我正在编写一个高低的纸牌游戏。目标是猜测第二张卡是否会比第一张大或小。该程序运行良好,除了一个问题。在游戏中,玩家以 5 个积分开始。每次猜测他都会获得一个积分,最多 10 分,如果他没有猜到,他会失去 1 分,降到 0 分。 在 0 或 10 时,他应该得到一个 You Win !或者你输了!消息,他确实这样做了,但问题是该程序无论如何都会进行一次额外的抽奖。

我先使用了 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");
            }

         }

     }
}

【问题讨论】:

    标签: java loops while-loop do-while


    【解决方案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;
         }
    }
    

    【讨论】:

    • 你是对的。这有帮助。非常感谢!
    【解决方案2】:

    将 You Win and 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;
                 }
    
    
    
    
             }
    

    【讨论】:

      【解决方案3】:
      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;
                   }
      

      如果满足条件,您可以考虑打破循环。移动条件将无济于事,无论如何它都会打印额外的数据。

      【讨论】:

        【解决方案4】:

        确保“抽奖”指令出现在“赢”或“输”之后,因为您没有返回到 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;
                     }
        

        【讨论】:

          【解决方案5】:

          试试这个代码:

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

          【讨论】:

            猜你喜欢
            • 2011-02-07
            • 1970-01-01
            • 2015-09-12
            • 2021-07-02
            • 1970-01-01
            • 1970-01-01
            • 2020-08-15
            • 1970-01-01
            • 2017-11-14
            相关资源
            最近更新 更多