This is the second version of the third day project! This one, instead of returning every single roll, summarizes the rolls for you.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Times to roll dice: ");
int numberOfDiceRolls = input.nextInt();
int rolledTimes = 1;
int[] rollTallies = new int[7];
while (numberOfDiceRolls >= rolledTimes) {
int randomRoll = (int)(Math.random() * 6) + 1;
rollTallies[randomRoll]++;
rolledTimes++;
}
System.out.println("Landed on 1: " + rollTallies[1]);
System.out.println("Landed on 2: " + rollTallies[2]);
System.out.println("Landed on 3: " + rollTallies[3]);
System.out.println("Landed on 4: " + rollTallies[4]);
System.out.println("Landed on 5: " + rollTallies[5]);
System.out.println("Landed on 6: " + rollTallies[6]);
}
}
To use this code is extremely simple. The steps are listed below, but the simple version is: Copy code, paste in an online compiler, run, and answer any questions the program gives you.
HOW TO USE (Step-by-Step)
1. First, you need to copy the code. Hover over the code, and a small “Copy” button will appear in the top-right. Copy the code with the button. If it’s not showing up, you can also highlight the code and copy it manually.
2. Next, you have to go to an online Java compiler. Programiz compiler is highly recommended, as that is where I build my code.
3. In the left panel, remove all the code that Programiz already has there by default, and paste my code in.
4. Click run, and your program should automatically run. Fill in any questions it asks you on the right panel.
Using the code is that simple! Every day, I’m going to post another one of these simple programs for a few days. Make sure to check in tomorrow for the next simple program, and have a good day!