为了账号安全,请及时绑定邮箱和手机立即绑定

经过一定次数的尝试后打印打印语句

经过一定次数的尝试后打印打印语句

aluckdog 2021-04-09 14:15:49
我是编码新手。我创建了一个猜谜游戏,并且运行良好,但是我想知道如何制作它,以便用户尝试猜测该数字3次后,他们会得到一个提示,我将其放在最后一行,但是目前无法实现,而且我不知道如何在do while循环中使该语句可访问。我目前陷入困境。谢谢import java.util.Scanner;public class guessing_game {   public static void main (String[] args) {      Scanner kb = new Scanner(System.in);      desc();      run(kb);          //int nun = 0;      //for (int i = 0; i < nun; nun ++)   }   public static void desc() {      System.out.println("This is a guessing game.");      System.out.println();      System.out.println("Let's see how many tries it takes you to guess the right number!");      System.out.println();      System.out.println();      System.out.println();   }   public static int run(Scanner kb) {      System.out.println("Please enter a number between 1-100");      int guess = kb.nextInt();      int num = 44;      int tries = 0;      do {         if (guess < num) {            System.out.println("Oooh. Your guess is too low. Try again.");            System.out.println();            run(kb);         }         else if ((guess > 100) || (guess < 0)) {            System.out.println("That isn't between 1-100 is it?");            System.out.println();            run(kb);         }         else if (guess > num) {            System.out.println("Aaah. Your guess is too high. Try again.");            System.out.println();            run(kb);         }         else if(guess == num) {            System.out.println("Bingo!!! Nice guess bud.");            System.out.println("Tell a friend to play! Wanna try again? (y or n)");            String choice = kb.next();            if (choice.equalsIgnoreCase("y")) {               run(kb);            }            else if (choice.equalsIgnoreCase("n")) {               System.exit(0);            }         }         tries++;      }while(tries < 3);      {         System.out.print("Here's a hint the lucky number is 4");      }      return guess;   }  }
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信