简体   繁体   中英

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.' 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:

Choose a birthday for the next person, uniformly at random between 0 and n−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%.

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. An example of command-line arguments and how the output is supposed to look: image

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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