简体   繁体   English

简单的 Java 程序不打印任何东西

[英]Simple Java program not printing anything

This is my first time posting here so please excuse any mistakes.这是我第一次在这里发帖,如有错误请见谅。 I am an absolute beginner at Java, and I have the task of programming the 'Birthday problem.'我是 Java 的绝对初学者,我的任务是编写“生日问题”。 It goes as follows:它是这样的:

Suppose that people enter a room one at a time.假设人们一次进入一个房间。 How people must enter until two share a birthday?人们必须如何进入直到两个人共享生日? Write a program Birthday.java that takes two integer command-line arguments n and trials and performs the following experiment, trials times:编写一个程序Birthday.java ,它需要两个 integer 命令行 arguments n并试验并执行以下实验,试验次数:

Choose a birthday for the next person, uniformly at random between 0 and n−1 .为下一个人选择一个生日,在0n−1之间均匀随机。 Have that person enter the room.让那个人进入房间。 If that person shares a birthday with someone else in the room, stop;如果该人与房间里的其他人共享生日,请停止; otherwise repeat.否则重复。 In each experiment, count the number of people that enter the room.在每个实验中,计算进入房间的人数。 Print a table that summarizes the results (the count i , the number of times that exactly i people enter the room, and the fraction of times that i or fewer people enter the room) for each possible value of i from 1 until the fraction reaches (or exceeds) 50%.打印一个表格,总结从1到分数达到的每个可能值i的结果(计数i 、恰好i人进入房间的次数以及i或更少人进入房间的次数) (或超过)50%。

The problem is, my code is not printing anything.问题是,我的代码没有打印任何东西。 It's compiling normally and not showing any errors, but when I try to run it it just doesn't show any output and I've run out of ideas as to what to do to improve it.它正在正常编译并且没有显示任何错误,但是当我尝试运行它时,它只是没有显示任何 output 并且我已经没有关于如何改进它的想法。 An example of command-line arguments and how the output is supposed to look: image命令行 arguments 的示例以及 output 的外观:图片

Here is my code:这是我的代码:

public class Birthday {

    public static void main(String[] args) {

        int n = Integer.parseInt(args[0]);
        int trials = Integer.parseInt(args[1]);
        boolean[] birthdays = new boolean[n];
        int[] visits = new int[n + 2];
        int cumsum = 0;
        int k = 1;

        for (int i = 1; i <= trials; i++) {
            for (int j = 0; j < n; j++) {
                birthdays[j] = false;
            }
            for (k = 1; k <= n; k++) {
                int r = (int)(Math.random() * (n - 1));

                if (birthdays[r] == false) {
                    birthdays[r] = true;
                    continue;
                } else
                if (birthdays[r] == true) {
                    visits[k]++;
                    cumsum += visits[k];
                    break;
                }
            }
        }

        int l = 1;

        while ((double)(cumsum / trials) <= 0.5) {
            System.out.print(l + "(\t)" + visits[k] + "(\t)" + ((double) cumsum / trials));
            l++;
            System.out.println("");
        }
    }
}      

Since the statements you expect to see now but do not get them are all within your while loop you should focus your thoughts on how/when the while loop executes.由于您现在希望看到但没有得到它们的语句都在您的 while 循环中,您应该将注意力集中在 while 循环如何/何时执行上。 Which essentially means to look at the condition, as others already suggested.正如其他人已经建议的那样,这实质上意味着查看情况。

Just to check whether your code is able to print you can add a print statement where it would run unconditionally.只是为了检查您的代码是否能够打印,您可以添加一个打印语句,它将无条件运行。 For example at program start, or before the while loop is entered.例如在程序开始时,或在进入 while 循环之前。

To focus on the condition it might be interesting to see the variable values used to decide the loop, so I'd add print statements before and inside the loop anyway.为了专注于条件,查看用于决定循环的变量值可能会很有趣,所以无论如何我都会在循环之前和内部添加打印语句。 If you do this properly you won't even need to debug your code.如果您正确地执行此操作,您甚至不需要调试您的代码。

Last and not least, learning how to debug code in small projects will definitely help you with big ones.最后同样重要的是,学习如何在小型项目中调试代码肯定会帮助您处理大型项目。

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

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