我想写一个程序,允许用户在7次尝试中猜测0到100之间的数字我不知道为什么这样不行

kupeojn6  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(249)
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int rnd = (int)(Math.random() * 101);

        System.out.println("The program is going to give a number that is between 0 and 100 (including them). You can guess it by pressing Run.");
        System.out.println("Enter your number:");
        int num = scan.nextInt();

        for (int count = 1; count <= 7; count++) {
            while (num != rnd) {
                if (num < rnd) {
                    System.out.println("Your guess is too low.");
                }
                if (num > rnd) {
                    System.out.println("Your guess is too high.");
                }
                if ((Math.abs(rnd - num) == 1) || (Math.abs(rnd - num) == 2)) {
                    System.out.println("But your guess is VERY close.");
                }
                num = scan.nextInt();
            }
            System.out.println("You got it right!");
        }
        System.out.println("You should guess it in 7 tries.");
    }
}

所以我用了两个循环,然后把它们嵌套起来。是这样的吗?现在的代码类似于从for循环开始,如果这是真的,那么它将转到while循环部分,在那里猜测数字。仅仅移动一些代码和修复次要区域就可以解决这个问题吗?

bvk5enib

bvk5enib1#

我认为你应该重建你的代码,让它更清楚。
拆分标题(带任务说明)
拆分主循环,从中读取用户输入并用期望的数字进行检查
分割最终结果的输出,打印结果。

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    final int rnd = new Random().nextInt(101);
    final int maxAttempts = 7;

    System.out.println("The program is going to give a number that is between 0 and 100 (including them).");
    System.out.println("You can guess it within maximum " + maxAttempts + " attempts by pressing Run.");
    boolean success = false;

    for (int attempt = 1; attempt <= maxAttempts && !success; attempt++) {
        System.out.format("(%s of %s) Enter your number: ", attempt, maxAttempts);
        int num = scan.nextInt();

        if (num == rnd)
            success = true;
        else {
            System.out.print("Your guess is too " + (num > rnd ? "high" : "low") + '.');
            System.out.println(Math.abs(rnd - num) <= 2 ? " But it's VERY close." : "");
        }
    }

    System.out.println(success ? "You got it right!" : "Bad luck this time. Buy.");
}
8dtrkrch

8dtrkrch2#

import java.util.Scanner;

class Main {
  public static void main(String[] args) 
  {
    Scanner scan = new Scanner(System.in);
    int rnd = (int) (Math.random() * 101);

    System.out.println("The program is going to give a number that is between 0 and 100 (including them). You can guess it by pressing Run.");
    System.out.println("Enter your number:");
    int num = scan.nextInt();

    for(int count = 1; count <= 7; count++)
    {
        if (num < rnd)
        {
          System.out.println("Your guess is too low.");
        }
        else if (num > rnd)
        {
          System.out.println("Your guess is too high.");
        }
        else if ((Math.abs(rnd - num) == 1) || (Math.abs(rnd - num) == 2))
        {
          System.out.println("But your guess is VERY close.");
        }
        else
          System.out.println("You got it right!");

        System.out.println("Enter Next number: ");
        num = scan.nextInt();
    }
  }
}

基本上你的推理是错误的,因为你把while循环放在for循环中。您只需要7次迭代,在这7次迭代中,您将根据用户输入检查条件。

tyu7yeag

tyu7yeag3#

在这种情况下,您应该手动编写代码。真的。拿一张纸假装你是一台电脑。这是一个很好的练习,它会帮助你解决你的问题。
问题是你的内环。它循环,直到他们猜测正确,无论尝试次数。然后你强迫他们用外环再做6次。
你真的只需要一个循环。我会有一个这样的循环:

int attempts = 0;
int num = 0;
do {
    num = scan.nextInt();
    ... most of the if code from your inner loop but not another scan.nextInt
} while (++attempts < 7 && num != rnd);
// and here you look at num == rnd to see if success or failures

相关问题