我如何让这个猜谜游戏起作用?

时间:2016-07-03 00:52:21

标签: java

我试图制作一个猜谜游戏,我提示用户输入一个密码,他有6次试图让它正确。我把它的大部分都弄了下来,但是一旦他用完了他的尝试我就不能告诉他了。对不起,你已经用完了,秘密号码是x& #34)

我的代码:

import java.util.*;
import java.util.Scanner;
public class GuessGame{
  static public void main(String[] args)
  {
    System.out.println("Welcome to the guessing game. I will pick a number between 1");
    System.out.println("and 100 and you will try to guess it in 6 tries or less. Here we go: ");

    for (int i = 6; i > 0; i--)
    {
      Random rand = new Random(); 
      int secNumber = rand.nextInt(5);
      int theNumber = secNumber;
      Scanner s = new Scanner(System.in);
      System.out.println("Enter a number: ");
      int guess = s.nextInt();

      if (guess == theNumber)
      {
        System.out.println("That's it! You guessed it, the number was: " +  theNumber + " and it took you " + (7 - i) + " tries.");
      }

      while(i > 0) {
      if (guess < theNumber)
      {
        System.out.println("The secret number is bigger than than that. You have " + (i - 1) + " tries left.");
        break;
      }
      else if (guess > theNumber)
      {
        System.out.println("The secret number is smaller than than that. You have " + (i - 1) + " tries left.");
        break;
      }

    }

告诉用户当他剩下零尝试然后揭示秘密号码时,我试了一下     而(i == 0)     {       的System.out.println(....);     } 但它在第一次猜测后立即开始。

2 个答案:

答案 0 :(得分:0)

while循环是不必要的,并且样式已关闭。这是我的修复。

import java.util.*;
import java.util.Scanner;


public class GuessGame {
  public static void main(String[] args) {
    System.out.println("Welcome to the guessing game. I will pick a number between 1");
    System.out.println("and 100 and you will try to guess it in 6 tries or less. Here we go: ");
    int theNumber = rand.nextInt(5);
    for (int i = 6; i > 0; i--) {
      Random rand = new Random();
      Scanner s = new Scanner(System.in);
      System.out.println("Enter a number: ");
      int guess = s.nextInt();

      if (guess == theNumber) {
        System.out.println("That's it! You guessed it, the number was: " +  theNumber + " and it took you " + (7 - i) + " tries.");
      } else if (guess < theNumber) {
        System.out.println("The secret number is bigger than than that. You have " + (i - 1) + " tries left.");
      } else if (i == 1) {
        System.out.println("You could not guess the number");
      } else {
        System.out.println("The secret number is smaller than than that. You have " + (i - 1) + " tries left.");
      } 
    }
  }
}

答案 1 :(得分:0)

  1. 不需要while循环。
  2. 在for循环之外分配secNumber
  3. 变量theNumber是不必要的。
  4. 您需要将rand.nextInt(5)替换为rand.nextInt(100) + 1才能获得1到100之间的数字。
  5. i - 1更改为i,因为猜测次数将等于i
  6. 从if块中移除break,因为这些会让您离开游戏。
  7. break添加到第一个if语句(如果他们赢了)
  8. 添加if语句让玩家知道游戏已结束。

    import java.util.*;
    import java.util.Scanner;
    
    public class GuessGame
    {
        public static void main(String[] args)
        {
            System.out.println("Welcome to the guessing game. I will pick a number between 1");
            System.out.println("and 100 and you will try to guess it in 6 tries or less. Here we go: ");
            Random rand = new Random();
            int secNumber = rand.nextInt(100) + 1;    // 2 4
            for (int i = 6; i > 0; i--)
            {
                Scanner s = new Scanner(System.in);
                System.out.println("Enter a number: ");
                int guess = s.nextInt();
    
                if (guess == secNumber)
                {
                    System.out.println("That's it! You guessed it, the number was: " +  secNumber + " and it took you " + (7 - i) + " tries.");
                    break;    // 7
                }
                else if (guess < theNumber)
                {
                    System.out.println("The secret number is bigger than than that. You have " + i + " tries left.");    // 5
                }
                else if (guess > theNumber)
                {
                    System.out.println("The secret number is smaller than than that. You have " + i + " tries left.");    // 5
                }
    
                if(i == 1)    // 8
                {
                    System.out.println("Out of tries.");
                }
            }
        }
    }