Measuring that roll Download

Christina Cacioppo

arrays, for loops, if statments Student approved


We've talked about how to get a series of dice rolls, but we didn't do much with those rolls. Until now!


Write the getLongestRun method in the Dice class that takes an int array representing a series of Dice rolls and returns the length of the longest run of consecutive numbers.

Starter code

import java.util.Random; 

class Dice{

	/** Returns the length of the longest run of repeated values
		in int[] rolls.
		@param	rolls – an array of integer values representing a
		series of dice rolls.
		@return – the length of the longest run.
	int getLongestRun(int[] rolls){
		/*		Your code here		*/

	int[] getRolls(int numRolls){
		int[] rolls = new int[numRolls];
		for(int i = 0; i < numRolls; i++){
			rolls[i] = roll();
		return rolls;

	int roll(){
		Random gen = new Random();
		return gen.nextInt(5) + 1;

Expected output

It'll depend on the (random) array you're working with, but two examples –  In this array, the length of the maximum run – in this case, of 2s – is 2:
But in this array, the "longest" run is 1:


Useful tools to solve the problem