【问题标题】:How to exit while loop with new value?如何以新值退出while循环?
【发布时间】:2017-09-29 11:08:59
【问题描述】:

对于我的一项任务,我们应该首先生成一个介于 1 和 13 之间的随机数,然后我们询问用户是否想绘制另一个数字。如果他们这样做,我们应该在 1-13 内添加另一个随机数而不超过 21。我坚持的是如何在添加 1 个新随机数后退出 while 循环并询问用户是否愿意添加另一个。我尝试使用 break;这是我目前所拥有的:

int randCard = 1 + (int) (Math.random() * ((13 - 1) + 1));
    int playerHand = 0 + randCard;
    System.out.println("START GAME #" + gameNum);
    System.out.println("Your card is a " + randCard + "!");
    System.out.println("Your hand is: " + playerHand);
    System.out.println("\n1. Get another card");
    System.out.println("2. Hold hand");
    System.out.println("3. Print statistics");
    System.out.println("4. Exit");
    System.out.print("\nChoose an Option: ");
    int selectedOption = menuOptions.nextInt();

 if (selectedOption == 1) {

             do {
                 int newRandCard = 1 + (int) (Math.random() * ((13 - 1) + 1));

                 System.out.println("Your card is a " + newRandCard + "!");
                 System.out.println("Your hand is: " + (playerHand + newRandCard));

                 playerHand = (playerHand + newRandCard);


                break;

             }
                 while (playerHand <= 21) ;

////////IDEALLY IT SHOULD PRINT LIKE THIS//////
Your card is a 4!
Your hand is: 4
1. Get another card
2. Hold hand
3. Print statistics
4. Exit
Choose an option: 1
Your card is a 9!
Your hand is: 13
1. Get another card
2. Hold hand
3. Print statistics
4. Exit

【问题讨论】:

  • 将询问放在循环中。逻辑是 -> 同时,玩家选择卡(是继续,不中断),随机卡,如果堆栈超过 21 中断,则将新卡添加到堆栈。

标签: java while-loop do-while


【解决方案1】:

如果你只需要它添加一次随机数,那么再给用户菜单选项

System.out.println("START GAME #" + gameNum);
int randCard = 1 + (int) (Math.random() * ((13 - 1) + 1));
System.out.println("Your card is a " + randCard + "!");
int playerHand = 0 + randCard;
System.out.println("Your hand is: " + playerHand);
do {
  System.out.println("1. Get another card");
  System.out.println("2. Hold hand");
  System.out.println("3. Print statistics");
  System.out.println("4. Exit");
  System.out.println("Choose an Option: ");
  int selectedOption = menuOptions.nextInt();

  if (selectedOption == 1) {
      randCard = 1 + (int) (Math.random() * ((13 - 1) + 1));
      System.out.println("Your card is a " + newRandCard + "!");
      System.out.println("Your hand is: " + (playerHand + newRandCard));
      playerHand = (playerHand + randCard);
  }
   ...other options to select
} while (playerHand <= 21) ;

【讨论】:

  • 如果我不使用break,程序一直在添加一个随机数,我只需要它添加一个随机数,然后再次给用户菜单选项。
  • 我将帖子编辑为理想打印的样子。
  • 您可以在selectedOption == condition 上添加break; 然后
  • 我尝试了休息; selectedOption == 4 内的方法,但它保留在循环内。如果不在里面,休息会去哪里?
  • 它会跳出它所在的循环......所以在我共享的代码中它会退出 do..while 循环。
【解决方案2】:

您可以将代码更改为以下内容以循环,直到用户做出除 1 以外的选择。

if (selectedOption == 1) {
     do {
         ...
     } while (playerHand <= 21 && selectedOption == 1);

【讨论】:

  • 嘿,我刚试过这个,它仍然在循环中。知道为什么吗?
【解决方案3】:

如果你只想抓一张牌,那么不要使用 do-while 循环。只需使用 do-while 中的内容(不休息)。

如果您想在用户告诉您之前抽牌,则创建一个布尔变量并将其用作 do-while 循环的条件,并在每个循环结束时读取它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2015-01-18
    • 2014-11-28
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多