6 Replies - 2014 Views - Last Post: 18 November 2012 - 05:09 AM 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 - 02:58 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 = 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

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: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Help with a dice roller and runs.

Posted 15 November 2012 - 03:05 PM

first
for (int i= 0; i <= size ; i++)
should be
for (int i= 0; i < size ; i++)
or you will get an Index Out of Bound Exception

diceRolls[i] = randomRoll.nextInt(6);
should probably be
diceRolls[i] = randomRoll.nextInt(6) + 1;

if you want numbers between 1 and 6 and not 0 and 5

the second
for (int i = 0; i <= size; i++)
should be
for (int i = 1; i < size; i++)
if not diceRoll[i-1] will generate an index out of bound exception
Was This Post Helpful? 0
  • +
  • -

#3 thof71  Icon User is offline

  • New D.I.C Head

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

Re: Help with a dice roller and runs.

Posted 15 November 2012 - 04:00 PM

Ok, got it fixed. But I'm still confused as to why random seems to not be working.

I'm getting:

run:
Please enter size: 20
12345(67)891011121314(1516)17(1819)BUILD SUCCESSFUL (total time: 2 seconds)


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Help with a dice roller and runs.

Posted 15 November 2012 - 04:06 PM

as nothing to do with the way random is working
as to do with the way you insert ( and )
Was This Post Helpful? 0
  • +
  • -

#5 thof71  Icon User is offline

  • New D.I.C Head

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

Re: Help with a dice roller and runs.

Posted 15 November 2012 - 05:57 PM

I'm assuming you mean the ( and ) that are actually printed, but I don't see the correlation between how those would affect the numbers that are generated...
Was This Post Helpful? 0
  • +
  • -

#6 thof71  Icon User is offline

  • New D.I.C Head

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

Re: Help with a dice roller and runs.

Posted 17 November 2012 - 10:48 AM

Can anyone help with this? I'm completely stuck.
Was This Post Helpful? 0
  • +
  • -

#7 huggen  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 32
  • Joined: 23-May 09

Re: Help with a dice roller and runs.

Posted 18 November 2012 - 05:09 AM

You aren't printing the values from the generation, you are printing the array index.

And just in this case the second for should be:
for (int i = 0; i <= size-1; i++)

as the unRun never will be true in first run.

public static void main(String[] arg) {

        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-1; 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(diceRolls[i]);
           
        }
        
        if(inRun)
        {
        	System.out.print(diceRolls[size-1]);
            System.out.print(")");
        }
        else {
        	System.out.print(diceRolls[size-1]);
        }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1