Java Hi-Lo猜谜游戏-限制用户输入的尝试次数&;重弹逻辑

Java Hi-Lo猜谜游戏-限制用户输入的尝试次数&;重弹逻辑,java,nested-loops,do-while,retry-logic,Java,Nested Loops,Do While,Retry Logic,我是Java编程新手,正在上一门大学课程,在这门课程中,我有一项任务是创建一个Hi/Lo猜谜游戏。该游戏为用户提供最多5次尝试输入1到100(含)之间的数字。程序必须提供回答是否过低、过高或正确的逻辑依据。该程序必须提供在获胜或5次尝试失败后再次播放的选项 我已经重新创建了这个程序大约10次:(.我无法让逻辑按照上面的说明工作。我无法在5次尝试时停止尝试…我无法让程序执行新游戏 非常感谢您的帮助。我花了无数时间编写和重新编写这段代码,得到了许多不同的结果——但不是预期的结果 这是我第一次发布,因

我是Java编程新手,正在上一门大学课程,在这门课程中,我有一项任务是创建一个Hi/Lo猜谜游戏。该游戏为用户提供最多5次尝试输入1到100(含)之间的数字。程序必须提供回答是否过低、过高或正确的逻辑依据。该程序必须提供在获胜或5次尝试失败后再次播放的选项

我已经重新创建了这个程序大约10次:(.我无法让逻辑按照上面的说明工作。我无法在5次尝试时停止尝试…我无法让程序执行新游戏

非常感谢您的帮助。我花了无数时间编写和重新编写这段代码,得到了许多不同的结果——但不是预期的结果

这是我第一次发布,因此,如果发布的格式不正确,我深表歉意

我浏览过的论坛和示例比我愿意承认的要多,而且我所审查和尝试实现的代码中没有一个能让我将用户输入限制为每次5次,并且能够多次播放

这是我的密码:

public class HiLoGuessingGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Initialize scanner and random number gennerator
        Scanner input = new Scanner(System.in); 
        Random generator = new Random();

        //State the rules of the game 
        System.out.println("The Hi-Lo Guessing Game. Guess a number between 1-100");
        System.out.println("You have 5 attempts!");

       /* define the variable Guess (user iput)
          define the variable Answer (random generator)
          define the variable Counter (track number of tries and limit to 5)
          define the variable PlayAgain (Y/N question)*/
        int guess = 0; 
        int answer = generator.nextInt(100)+1;
        int counter = 1;
        String playAgain;
        boolean gameOver = false;

        //Ask the Hi-Lo question - pick number 1-100 (inclusive) 
        //Provide feedback answer too high, too low or you win! 
        //Limit number of tries in the game to 5 

        while (guess != answer) {
            System.out.print("Enter your guess: ");
            guess = input.nextInt(); 
            counter++;

            if (guess < answer)  {
                 System.out.println("Your guess " + guess + " is too low. Try again");
                 System.out.println("This is attempt: " + counter);
            } else if (guess > answer) {
                 System.out.println("Your guess " + guess + " is too high. Try again");
                 System.out.println("This is attempt: " + counter);
            } else if (guess == answer) {
                 System.out.println("Your guess " + guess + " is correct! You win!");
                 System.out.println();
                 System.out.println("Would you like to play again (Y/N)?");
                 playAgain = input.next();
            } 

        }
        if (counter ==6) {
            System.out.println("Sorry, you've reached your max atttempts.");
            System.out.println("Would you like to play again (Y/N)?"); 
            playAgain = input.next();
        }

        // Play again logic
        boolean isValid;
        do {
            System.out.print("Would you like to play again (Y/N)?");
            playAgain = input.next().toUpperCase();
            isValid = playAgain.equals("Y") || playAgain.equals("N");
            playAgain = input.next();
            counter = 1;
            if ( !isValid ) {
                 System.out.println("Error, please enter Y or N");
                 System.out.println();
            }
        } while (!isValid);
    }
}
公共类山丘游戏{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//初始化扫描仪和随机数生成器
扫描仪输入=新扫描仪(System.in);
随机生成器=新随机();
//陈述游戏规则
System.out.println(“Hi-Lo猜游戏,猜1-100之间的数字”);
System.out.println(“您有5次尝试!”);
/*定义变量猜测(用户iput)
定义变量答案(随机生成器)
定义变量计数器(跟踪尝试次数并限制为5)
再次定义变量(是/否问题)*/
int guess=0;
int-answer=生成器。nextInt(100)+1;
int计数器=1;
再次演奏弦乐;
布尔gameOver=false;
//询问Hi-Lo问题-选择编号1-100(含)
//提供反馈回答太高、太低,否则你会赢!
//将游戏中的尝试次数限制为5次
while(猜!=回答){
System.out.print(“输入您的猜测:”);
guess=input.nextInt();
计数器++;
如果(猜测<回答){
System.out.println(“您的猜测“+猜测+”太低,请重试”);
System.out.println(“这是尝试:+计数器);
}else if(猜测>回答){
System.out.println(“您的猜测“+猜测+”太高,请重试”);
System.out.println(“这是尝试:+计数器);
}else if(猜测==答案){
System.out.println(“你的猜测”+guess+“是正确的!你赢了!”);
System.out.println();
System.out.println(“您想再次播放吗(Y/N)”;
playreach=input.next();
} 
}
如果(计数器==6){
System.out.println(“对不起,您已达到最大值。”);
System.out.println(“您想再次播放吗(Y/N)”;
playreach=input.next();
}
//重弹逻辑
布尔值是有效的;
做{
System.out.print(“您想再次播放吗(Y/N)”;
再次播放=input.next().toUpperCase();
isValid=playreach.equals(“Y”)| playreach.equals(“N”);
playreach=input.next();
计数器=1;
如果(!isValid){
System.out.println(“错误,请输入Y或N”);
System.out.println();
}
}而(!isValid);
}
}

您可以在while循环中添加额外的条件:

while (guess != answer && counter < 5) {
    // ...
}
while(猜测!=回答和计数器<5){
// ...
}
或者,当你得到正确答案时,你可以打破循环:

 while (counter < 5) {
    // ...
    if (answer == guess){
       // ...
       break;
    } 
 } 
while(计数器<5){
// ...
如果(答案==猜测){
// ...
打破
} 
} 

您必须检查while循环中的计数器,而不是它后面的计数器。
while(guess!=answer&&counter!=6)
这样,如果其中一个条件未被验证,while循环将停止。您可以让
if(counter==6){…}
为了在播放器松动的情况下保持打印逻辑,您应该删除第二个System.out.println,并按照您将在do中执行的方式读取输入…while block谢谢。我做了这些更改,它们现在似乎可以工作了。唯一的问题是让逻辑重新运行:)请参阅,谢谢您的回复。我创建了中断循环-因为这是有意义的。我还尝试了计数器<5逻辑,这也很有效。非常感谢。我能够得到重试的最大尝试停止在5-谢谢!现在,我正在研究重试(playreach)逻辑,但仍然无法成功实现。下面是代码的一部分。我不知道如何修改我原来的帖子-对不起。这是我第一次在这里发帖:我在while(playing==true)时将整个块包装在一个新的while语句中,然后以://Play-each-logic-System.out.print(“您想再次播放(Y/N)”结束;String play=input.nextLine();while(playing=play.equalsIgnoreCase(“y”){System.out.println();guess=input.nextInt();guess=0;answer=generator.nextInt(100)+1;计数器=0;