Home>

I want to make a program that rolls two dice and outputs the total of the eyes.
Use self-made function called getRandomInt as a condition.

Error message
The execution result will be as follows.


Applicable source code
public class Ex08_01 {
    static double getRandomDouble (double low, double high) {
        double ans = 0.0;
        double r = Math.random ();
        ans = low + r * high;
        return ans;
    }
    static int getRandomInt (int low, int high) {// use this function
        int ans = 0;
        ans = (int) getRandomDouble (low, high);
        return ans;
    }
    public static void main (String [] args) {
        int i, total;
        int [] d = {0,0,0,0,0,0,0,0,0,0,0};
        i = 0;
        total = 0;
        while (i<3600) {// 3600 shakes
            int x = getRandomInt (0, 6);// Dice X
            int y = getRandomInt (0, 6);// Dice Y
            total = x + y;
            d [total] ++;
            i ++;
        }
        System.out.println ("When you roll two dice");// Outputs how many times each eye has been added
        System.out.println ("The total number of eyes was 2" + d [0] + "times");
        System.out.println ("The total number of eyes was 3" + d [1] + "times");
        System.out.println ("The total number of eyes was 4" + d [2] + "times");
        System.out.println ("The total number of eyes was 5" + d [3] + "times");
        System.out.println ("The total number of eyes was 6" + d [4] + "times");
        System.out.println ("The total number of eyes was 7" + d [5] + "times");
        System.out.println ("The total number of eyes was 8" + d [6] + "times");
        System.out.println ("The total number of eyes was 9" + d [7] + "times");
        System.out.println ("The total number of eyes was 10" + d [8] + "times");
        System.out.println ("The total number of eyes was 11" + d [9] + "times");
        System.out.println ("The total number of eyes was 12" + d [10] + "times");
        }
}

I researched on the Internet, but it was not possible to solve it because it was in the condition of using my own function.

Supplemental information (FW/tool version etc.)

If i have any questions that are not enough, we are looking forward to correcting them.

Although there may be a fundamentally strange place for programming beginners, please be careful.

  • Answer # 1

    As swordone says, the code you presented was wrong with the code when you presented the result. When executed with the presented code, it looks like this: Thank you for many answers.

  • Answer # 2

    2 so
    d [total] ++;⇒ d [total-2] ++;
    Otherwise, d [0] will not have a value.

  • Answer # 3

    Output how many times the total of the // th each was output
    ↑↑↑↑↑ There is a full-width space here.


    Programming fonts should be included because it makes it easier to recognize 0O 1Il etc. and reduces stress.
    There is something that makes full-width spaces appear like this.

    Isn't it nice to have "Shirogen" right now? (Not a party)
    [Character width half-width 3: full-width 5 also added] I admired Ricty as a god font, a story that made a free license programming font "Shiragen"-Qiita