当前位置: 首页 > 知识库问答 >
问题:

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

田阳泽
2023-03-14
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循环部分,在那里猜测数字。仅仅通过移动一些代码和修复周围的小区域就可以解决这个问题吗?

共有3个答案

赫连永怡
2023-03-14
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.");
}
岳玉堂
2023-03-14
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();
    }
}

应该能正常工作。

岳飞航
2023-03-14

在这种情况下,您应该手动执行代码。字面意思。拿张纸假装你是一台电脑。这是一个很好的练习,它会帮助你解决你的问题。

问题是你的内循环。它循环直到他们猜对为止,而不考虑尝试的次数。然后你强迫他们用外圈再做6次。

你真的只需要1个循环。我会有一个这样的循环:

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
 类似资料:
  • 有一个源代码 基于下面的BoardController,我尝试测试接收pageable对象和接收menuId作为参数的方法,以检索文章的整个内容。 在BoardControlllerTest中创建PageRequest对象后,我创建了BoardDto列表,并使用PageImpl方法创建了Page对象。 (很抱歉没有帖子。) 如果您以相同的方式向web上的地址栏发送请求,它将正常发送,但如果您只是测

  • 我正在尝试导出可运行的文件,但每次我这么做时,它都会说,“VM参数将不是可运行jar的一部分。在启动jar时,可以在命令行上传递参数。” 有人知道怎么修吗?

  • 列表声明: 在代码的某个地方,我试图在这个列表上运行for循环: 这对我来说不太清楚。为什么我需要加上“;;”?我也不确定它是否能正常运行。

  • 我正在尝试编写一个程序,允许用户输入一系列整数形式的考试分数。 我希望输出看起来像: 输入整数,或输入 -99 退出:80 输入一个整数,或-99退出:95 输入整数,或-99退出:65 输入整数,或-99退出:-99 最大: 95最小: 65 第二轮: 输入整数,或-99退出:-99 您没有输入任何数字。 我把第一部分记下来了,但是当我输入-99时,我似乎不知道如何得到“你没有输入任何数字”行。

  • 我对这段代码有一个问题,因为我似乎找不到问题所在?这是我试图解决的问题:-声明并编写一个名为valid_triangle的函数,它将表示三角形三边长度的三个实数作为参数,并根据这三个长度是否能够构成三角形输出真或假。 关于三角形的以下规则: 三角形只能有正长度的边 三角形任意两条边的长度之和必须大于第三条边的长度

  • 我有组织。postgresql。util。PSQLException:错误:关系“roles”不存在,我不知道为什么。 实体类 资源/META-INF/持久性。xml 在我有 那么为什么我有这样的错误,为什么会这样?我读过这个问题,它对我没有帮助。

  • 问题内容: 当我使用jdk 7运行Json测试程序时,它说: 我在项目中包含了“ javax.json-api-1.0.jar”。 这是一个简单的程序,没有使用galssfish,为什么在这里提到玻璃鱼? 问题答案: 仅包含用于编译时依赖的API。但是,如果要运行您的应用程序,则需要一个类。 是您需要的同时包含+ 类的jar 。 有关更多详细信息,请参见此线程。