Help with a dice roller and runs.

Page 1 of 1

1 Replies - 190 Views - Last Post: 15 November 2012 - 03:15 PM Rate Topic: -----

#1 thof71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-November 12

Help with a dice roller and runs.

Posted 15 November 2012 - 03:14 PM

Hey all, so I'm in a beginners java class and I'm struggling lately. I'm turning to DiC because my teacher is unreachable and work conflicts with the tutor. So here's my assignment:

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 = false
If not inRun
If values[i] is the same as the following value
Print (.
inRun = true
Print values[i]
If inRun, print ).


Along with this 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.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with a dice roller and runs.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Help with a dice roller and runs.

Posted 15 November 2012 - 03:15 PM

Double posts
Please avoid souble threads
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1