A run is a sequence of adjacent repeated values. Write a program that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this:
1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1
Use the following pseudocode:
Set a boolean variable inRun to false.
For each valid index i in the array
If inRun
If values[i] is different from the preceding value Print ).
inRun = falce
If not inRun
The reason that I'm doing this with a Scanner for the size of the array is because the teacher assigned an additional assignment to make it work with ArrayLists, and using the Scanner was part of that.
Before the code I should mention once I get it working I will probably change it up so that the different operations are in their own methods.
Here's the code that I've done so far:
package diceroller;
import java.util.Scanner;
import java.util.Random;
public class DiceRoller {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random randomRoll= new Random();
System.out.print("Please enter size:");
int size = in.nextInt();
int [] diceRolls = new int [size];
for (int i= 0; i <= size ; i++)
{
diceRolls[i] = randomRoll.nextInt(6);
}
boolean inRun = false;
for (int i = 0; i <= size; i++)
{
if ( inRun == true )
{
if (diceRolls[i] != diceRolls[i-1])
{
System.out.print(")");
inRun = false;
}
}
if (inRun == false)
{
if(diceRolls[i] == diceRolls[i+1])
{
System.out.print("(");
inRun = true;
}
}
System.out.print(i);
}
if(inRun)
{
System.out.print(")");
}
}
}
I don't really understand two things.
1) Every time I try to run it I don't get random numbers for my elements, it just counts up from 0.
2) It may be because my elements in the array are not being randomized correctly, but the runs are not being processed and marked with the () correctly

New Topic/Question
Reply



MultiQuote




|