繁体   English   中英

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

[英]How do I get this guessing game to work?

我正在尝试制作一个猜谜游戏,在该游戏中,我提示用户输入一个秘密数字,而他有6次尝试将其正确设置。 我已经掌握了大部分内容,但是一旦他用尽了所有的努力我就无法告诉他“对不起,您用完了,机密数字是x”)

我的代码:

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(....); 但是,它在第​​一个猜测之后立即生效。

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. while循环是不必要的。
  2. 在for循环外分配secNumber
  3. 变量theNumber是不必要的。
  4. 您需要将rand.nextInt(5)替换为rand.nextInt(100) + 1 ,以使数字介于1和100之间(含1和100)。
  5. i - 1更改为i因为猜测计数将等于i
  6. 从if块中删除break ,因为这些会使您离开游戏。
  7. 在第一个if语句中添加break (如果他们赢了)
  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."); } } } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM