Results 1 to 9 of 9
Thread: Java Program (rolling the dice)
- 02-28-2010, 10:37 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
Java Program (rolling the dice)
I'm not even sure if this is where i'm supposed to ask this.
And i also know this is really simple, but i missed the first two weeks of class as i registered late and all of a sudden i have homework assignments. And i have no idea what to do.
The question that's asked is:
Write a program that simulates rolling two dice using the following steps:
Prompt the user for the number of sides for two dice.
“Roll” the dice three times by generating a random number between 1 (inclusive) and the number of sides (inclusive).
Keep track of the sum of the rolls for each die and output the sum and average for each die.
and this is how much i've gotten so far
// Subhan Jamil
// dice.java
import java.util.Random;
public class dice {
public static void main (String[] args) {
int die1;
int die2;
java.util.Scanner sc = new java.util.Scanner(System.in);
int num_sides = Scanner.nextInt();
If any 1 can please help me, or better yet talk to me personally to teach me a little i would greatly appreciate it.
-
This won't work:
Because you're trying to call a Scanner method off of the class, Scanner, and you can only do that with static methods. Instead you must call nextInt off of a Scanner object, but fortunately, you have created just such a creature on the line above. So to fix your current problem do this:Java Code:java.util.Scanner sc = new java.util.Scanner(System.in); int num_sides = Scanner.nextInt();
Java Code:java.util.Scanner sc = new java.util.Scanner(System.in); //int num_sides = Scanner.nextInt();// no good. Can't call nextInt() off of the Scanner class int numSides = sc.nextInt(); // better -- you can call nextInt() off of a Scanner object, scWe'll be happy to help you with errors in your code, but you'll find that the limitations of the forum and of our other time commitments (we're all volunteers after all), mean that we can't be a substitute for your teacher or a decent tutor, or for your studying and learning. And that's the first thing you must do: study and learn from your notes and your books before you attempt to start coding, else you're in for much pain.If any 1 can please help me, or better yet talk to me personally to teach me a little i would greatly appreciate it.
Good luck!
- 02-28-2010, 10:49 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
-
- 02-28-2010, 10:52 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
-
No problem. Keep working at your program and please feel free to come on back to show us any further problems you may have with the code or any specific questions you may have, but remember, the more specific the question, the better usually our answer. We'll be happy to help. Again best of luck to you!
- 02-28-2010, 10:57 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
Thx, I realized it was wrong to ask to do the thing. AS i won't learn anything, good response and friendly. :)
- 03-01-2010, 08:24 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
A start
public class dice{ ... }
first question what compiling program are you using that allows you to put the name of the class in lowercase? or did you just throw something together in hopes of someone replying to this and helping you??
first...
i would make the class die not dice you can create 2 instance of the die which will make 2 dice.
public class Die{
private int numberOfSides;
private int numberOneRolled;
private int numberTwoRolled; the numbers rolled can be determined by
a pseudo-random number generator
found in the API--Random.
here is the link for that... Java 2 Platform SE v1.4.2
look over the specification for creating a new random.
next you need a constructor to make a die.
public Die(int numberOfSides){
this.numberOfSides = numberOfSides; <-- initializes the die with the
specified number of sides and sets
that number to the instance nOSides
}
next queries:
/**
*returns the number of sides the die has.
*/
public int numberOfSides(){
return numberOfSides;
}
/**
*returns the first number rolled
*/
public int numberOneRolled(){
return numberOneRolled;
}
^^ same for numberTwoRolled
next commands:
/**
*this command sets the number of sides the die has.
*/
public void setNumberOfSides(int number){
this.numberOfSides = number;
}
/**
*this command rolls the dice.
*/
public void rollDice(Die die1, Die die2){
Random r1 = new Random();
if (die1.numberOfSides() < r1.nextInt(numberOfSides) + 1){
die1.numberOfSides() = numberOfSides - 1;
}
}
}
you need to create a TUI after this but this is the just and i hope it will get you going in the right direction.
- 03-01-2010, 03:15 PM #9
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Dice game issue- any Java gamblers ableto help?:P
By mambalamba in forum New To JavaReplies: 2Last Post: 12-17-2009, 06:49 PM -
Help with a dice game.
By hero in forum AWT / SwingReplies: 14Last Post: 07-26-2009, 11:50 AM -
Log4j problem - Logs are rolling into the previous date file
By vaibhavborole in forum Advanced JavaReplies: 0Last Post: 04-16-2009, 03:33 PM -
Small Dice Program
By kimmelim in forum New To JavaReplies: 13Last Post: 02-15-2009, 01:01 AM -
java help please- two sided dice
By pat8 in forum Advanced JavaReplies: 3Last Post: 07-01-2008, 02:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks