Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 21st, 2009, 11:37 PM
lepetitchou lepetitchou is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 2 lepetitchou User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 39 sec
Reputation Power: 0
Die rolling program?

Hey everyone, I'm EXTREMELY new to Java. I've been working on two programs, the first gets and sets random numbers (1-6) and the second grabs the random numbers from the first program, then displays which of the two rolls is higher.

here's the first:

Code:
import java.util.*;
import java.util.Random;
public class Die 

{	
	private int value;
 		public int getRoll()
	{
	return value;
	}
		public void setRoll 
		(int HIGHEST_DIE_VALUE, int LOWEST_DIE_VALUE )
	{
	int value = ((int) (Math.random() * 100 ) % 6 + 1);
	}
}


the second is:

Code:
public class twoDice2 {

   	public static void main(String[] args)
	{
		int FirstRoll;
		int SecondRoll;	
		Die D = new Die();
		
		FirstRoll = D.getRoll();
		SecondRoll = D.getRoll();
		
		System.out.println("The first roll: " + FirstRoll );
		System.out.println("The second roll: " + SecondRoll );
		      
      if( FirstRoll == SecondRoll )
      {
              System.out.println("The dice are equal");
      }
      else
      	if( FirstRoll > SecondRoll )
      	{
      		System.out.println("Die one wins");
      	}
      else
      	if( FirstRoll < SecondRoll )
      	{
      		System.out.println("Die two wins");
      	}
}



They both compiled properly, but only show the rolls as always equal, and always 0.

Please help if you can, thanks!

Reply With Quote
  #2  
Old October 27th, 2009, 01:54 PM
paulio84 paulio84 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 2 paulio84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 20 sec
Reputation Power: 0
Hello there,

The reason the result only returns zero (0) is because you never actually call the setRoll method when you set your new Die in the twoDice2 class. So therefore a random roll of the dice is never performed.

A quick fix would be to do this for your twoDice2 class
Code:
public class twoDice2 {

   	public static void main(String[] args)
	{
		int FirstRoll;
		int SecondRoll;	
		Die D = new Die();
		
                D.setRoll();
		FirstRoll = D.getRoll();
                D.setRoll();
		SecondRoll = D.getRoll();
		
		System.out.println("The first roll: " + FirstRoll );
		System.out.println("The second roll: " + SecondRoll );
		      
      if( FirstRoll == SecondRoll )
      {
              System.out.println("The dice are equal");
      }
      else
      	if( FirstRoll > SecondRoll )
      	{
      		System.out.println("Die one wins");
      	}
      else
      	if( FirstRoll < SecondRoll )
      	{
      		System.out.println("Die two wins");
      	}
}


Alternatively,

you could change the die class to just have one method to set and return the value (call the method doRoll or something) which would set the value just like setRoll and return it just like getRoll.

This way when you do
Code:
FirstRoll = D.getRoll();
SecondRoll = D.getRoll();


in your original code
you could now do
Code:
FirstRoll = D.doRoll();
SecondRoll = D.doRoll();


and then you should get two different numbers

HTH

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Die rolling program?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap