用户输入以Java重复程序

时间:2014-10-14 18:19:20

标签: java loops user-input

我正在编写一个简单的猜谜游戏程序,用户将输入一个数字来尝试猜测随机生成的数字。

如果他们得到了正确的号码,我想给他们再次播放的选项。

这是我的代码:

public class GuessingGame {
    private Random num = new Random();       
    private int answer = num.nextInt(10);   
    private int guess;  
    private String playAgain;  

    public void inputGuess(){
        System.out.println("Enter a number between 1 and 10 as your first guess: ");
        Scanner input = new Scanner(System.in);
        guess = input.nextInt(); 
        do{
        if (guess < 1 || guess > 10){
            System.out.println("That is not a valid entry. Please try again: ");
            guess = input.nextInt();
        }else if (guess > answer){
            System.out.println("Too high, Try Again: ");
            guess = input.nextInt();
        }else if (guess < answer){
            System.out.println("Too low, Try Again: ");
            guess = input.nextInt();
        }

        }while (guess != answer);

        System.out.println("Congratulations, You guessed the number!");
        System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
        playAgain = input.nextLine();
        if(playAgain == "Y" || playAgain == "y"){
            System.out.println("Enter a number between 1 and 10 as your first guess: ");
            guess = input.nextInt(); 
        }

    }
}

游戏正在播放但是当提示用户再次播放时没有任何事情发生?

有什么建议吗?

8 个答案:

答案 0 :(得分:2)

执行以下操作,您的代码将起作用:

  1. 将所有input.nextInt();替换为Integer.parseInt(input.nextLine());
  2. (playAgain == "Y" || playAgain == "y")替换为(playAgain.equalsIgnoreCase("Y"))
  3. 首先在answer内初始化inputGuess()
  4. if(playAgain.equalIgnoreCase("Y"))
  5. 替换inputGuess();正文

    当您通过控制台输入整数值时,它还包含\n(下一行)。但是当您使用nextInt()时,它不会读取此\n,但是当您尝试使用input.nextLine()获取下一行时,它会查找\n(下一行) line)已经存在于整数条目之后并且之后没有任何内容。代码寻找&#34; Y&#34;或&#34; y&#34;并打破,因为它没有找到任何一个。

    这就是Integer.parseInt(input.nextLine());在这里工作的原因

答案 1 :(得分:2)

这是完整的代码,完全正常工作和测试...不使用递归..并且一切都已修复。

    public static void main(String[] args)
    {
    String playAgain = "";
    Scanner scan = new Scanner(System.in);

    do
    {
        ClassName.inputGuess();
        System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
        playAgain = scan.nextLine();
    }
    while(playAgain.equalsIgnoreCase("Y"));
    System.out.println("Thanks for playing!");
}

public void inputGuess()
{
    Random num = new Random();
    int answer = num.nextInt(10)+1;
    Scanner input = new Scanner(System.in);
    int guess;

    System.out.println("Enter a number between 1 and 10 as your first guess: ");
    guess = input.nextInt(); 

    do
    {
        if (guess < 1 || guess > 10)
        {
            System.out.println("That is not a valid entry. Please try again: ");
            guess = input.nextInt();
        }
        else 
            if (guess > answer)
            {
                System.out.println("Too high, Try Again: ");
                guess = input.nextInt();
            }
            else 
                if (guess < answer)
                {
                    System.out.println("Too low, Try Again: ");
                    guess = input.nextInt();
                }
        input.nextLine();

    }
    while (guess != answer);

    System.out.println("Congratulations, You guessed the number!");
}

答案 2 :(得分:1)

以下是代码:

private Random num = new Random();

private int answer = num.nextInt(10) +1;

private int guess;

private String playAgain;

Scanner input = new Scanner(System.in);

public void inputGuess(){
    System.out.println("Enter a number between 1 and 10 as your first guess: ");

    guess = input.nextInt();
    do{
        if (guess < 1 || guess > 10){
            System.out.println("That is not a valid entry. Please try again: ");
            guess = input.nextInt();
        }else if (guess > answer){
            System.out.println("Too high, Try Again: ");
            guess = input.nextInt();
        }else if (guess < answer){
            System.out.println("Too low, Try Again: ");
            guess = input.nextInt();
        }

        if(guess == answer) {
            System.out.println("Congratulations, You guessed the number!");
            System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
            playAgain = input.nextLine();
        }

    }while (!playAgain.equals("Y") && !playAgain.equals("y"));
}

你只需要在while内引入输赢逻辑,条件就是结束/继续标志。

在比较字符串以使用 equals 方法时,另一件事总是要记住,因为==将比较对象引用而不是String值,在某些情况下==将返回true表示相等的字符串因为JVM如何存储字符串,但总是要使用等于

答案 3 :(得分:0)

一个问题

请勿按==比较两个字符串的内容 你应该使用equals()

想象一下,在这种情况下正确的答案是

将此作为蓝图样本

  int answer = 0;
  String yes = "";
  Scanner input = new Scanner(System.in);
  do{
    do{
        System.out.println("Enter your number");
        answer = input.nextInt();
  } while ( answer != 1);
      System.out.println("Would you like to play again?");
      yes = input.next();
  } while ( yes.equalsIgnoreCase("yes"));

输出:

enter image description here

答案 4 :(得分:0)

尝试这样的事情:

public void inputGuess(){
        System.out.println("Enter a number between 1 and 10 as your first guess: ");
        Scanner input = new Scanner(System.in);
        guess = input.nextInt();
        playAgain = "Y";
        do{
        if (guess < 1 || guess > 10){
            System.out.println("That is not a valid entry. Please try again: ");
            guess = input.nextInt();
        }else if (guess > answer){
            System.out.println("Too high, Try Again: ");
            guess = input.nextInt();
        }else if (guess < answer){
            System.out.println("Too low, Try Again: ");
            guess = input.nextInt();
        }

        if(guess == answer)
        {
            System.out.println("Congratulations, You guessed the number!");
            System.out.println("Would you like to play again? Enter Y to play or N to quit: ");
            input.nextLine();
            playAgain = input.next();
            answer = num.nextInt(10);
            guess = -1;
            if(!playAgain.equalsIgnoreCase("N"))
            {
                System.out.println("Enter a number between 1 and 10 as your first guess: ");
                guess = input.nextInt();
            }
        }

        }while (!playAgain.equalsIgnoreCase("N"));

    }

您需要使用代码检查是否要在循环内再次播放。这样你就等到他们正确猜到了这个号码,然后问他们是否想再玩一次。如果他们没有你退出循环,他们会重新开始这个过程。

答案 5 :(得分:0)

我上面看到的一些解决方案并不正确。随机数,你需要加1来得到1到10之间,你还需要与equals进行比较。我在这里使用不区分大小写。

以下代码可根据您的需要使用。

import java.util.Random;
import java.util.Scanner;

public class Test2 {
    private static Random num = new Random();
    private static int answer = 0;
    private static int guess;
    private static String playAgain;

    public static void main(String[] args) {
        inputGuess();
    }

    // Guess Method.
    public static void inputGuess(){
        // create answer.
        answer = 1+ num.nextInt(10);

        System.out.println("Enter a number between 1 and 10 as your first guess: ");
        Scanner input = new Scanner(System.in);
        guess = input.nextInt(); 
        do{
        if (guess < 1 || guess > 10){
            System.out.println("That is not a valid entry. Please try again: ");
            guess = input.nextInt();
        }else if (guess > answer){
            System.out.println("Too high, Try Again: ");
            guess = input.nextInt();
        }else if (guess < answer){
            System.out.println("Too low, Try Again: ");
            guess = input.nextInt();
        }

        }while (guess != answer);

        System.out.println("Congratulations, You guessed the number!");
        System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
        playAgain = input.nextLine();
        if( playAgain.equalsIgnoreCase("Y") ){ 
            inputGuess();
        }

    }
}

答案 6 :(得分:0)

到目前为止,您已经猜到了正确的方法。以下是我将如何处理它

public class Test {
    public static void main (String...a) {
        inputGuess();
    }
    public static void inputGuess() {

        Scanner input = new Scanner(System.in);
        String playAgain = "Y";
        int guess;
        Random ran = new Random();
        int answer = ran.nextInt(10) + 1;
        while (playAgain.equalsIgnoreCase("Y")) {
            System.out.println("Enter a number between 1 and 10 as your first guess: " + answer);
            guess = input.nextInt(); 
            do {
                if (guess < 1 || guess > 10) {
                    System.out.println("That is not a valid entry. Please try again: ");
                    guess = input.nextInt();
                } else if (guess > answer) {
                    System.out.println("Too high, Try Again: ");
                    guess = input.nextInt();
                } else if (guess < answer) {
                    System.out.println("Too low, Try Again: ");
                    guess = input.nextInt();
                }
            } while (guess != answer);

            System.out.println("Congratulations, You guessed the number!");
            System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
            input.nextLine();
            playAgain = input.nextLine();
            answer = ran.nextInt(10) + 1
        }           
    }
}

答案 7 :(得分:0)

此代码可以满足您的目的......

import java.util.Random;
import java.util.Scanner;

public class GuessingGame 
{
private Random num = new Random();
private int answer ;
private int guess;
private String playAgain;

public void inputGuess() 
{         
    answer = num.nextInt(11);
    System.out.println("Enter a number between 1 and 10 as your first guess: ");
    Scanner input = new Scanner(System.in);
    guess = input.nextInt();
    do {            
        if (guess < 1 || guess > 10) {
            System.out
                    .println("That is not a valid entry. Please try again: ");
            guess = input.nextInt();
        } else if (guess > answer) {
            System.out.println("Too high, Try Again: ");
            guess = input.nextInt();
        } else if (guess < answer) {
            System.out.println("Too low, Try Again: ");
            guess = input.nextInt();
        }

    } while (guess != answer);

    System.out.println("Congratulations, You guessed the number!");
    System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
    do
    {           
        playAgain = input.nextLine();

    }while(playAgain.length()<1);

    if (playAgain.trim().equalsIgnoreCase("y"))
    {
        inputGuess();           
    }
    else
    {
        System.out.println("Good Bye!!!");
    }

}

public static void main(String[] args) {
    new GuessingGame().inputGuess();
}
   }