View previous topic :: View next topic |
Author |
Message |
ravicus Master Cheater Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Mon Mar 19, 2007 2:57 pm Post subject: [Source Code] My Java Dice Roller. |
|
|
Hey guys, I just programmed this Java dice roller after reading about loops in my book, please comment.
Code: | //Imports
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;
public class diceroller {
public static void main(String[] args) {
//Declarations
int die1 = 0;
int die2 = 0;
int mynumber = 0;
int timesrolled = 0;
Random randomizer = new Random();
Scanner s = new Scanner(System.in);
//Begin Main Code
//Ask the user for a number
out.print ("Please enter a number 2-12 for the two dice to roll: ");
mynumber = (s.nextInt());
//User error checking loop
while ((mynumber) > 12 || (mynumber) < 2) {
out.println ("You entered an invalid number.");
out.print ("Please enter a number 2-12 for the two dice to roll: ");
mynumber = (s.nextInt());
}
out.println ("Thank-you, let the dice rolling begin!");
//Start the dice rolling loop
while ((die1) + (die2) != (mynumber)) {
die1 = randomizer.nextInt(6) + 1;
die2 = randomizer.nextInt(6) + 1;
out.print (die1);
out.print (" ");
out.println (die2);
timesrolled++;
}
//Ending Message
out.print ("Successfully rolled ");
out.print (mynumber);
out.print (" in ");
out.print (timesrolled);
out.print (" rolls");
}
}
|
_________________
|
|
Back to top |
|
|
DeltaFlyer Grandmaster Cheater Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Mon Mar 19, 2007 4:00 pm Post subject: |
|
|
Use Math.random()
and you really should get your indentations right.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
Back to top |
|
|
ravicus Master Cheater Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Mon Mar 19, 2007 4:29 pm Post subject: |
|
|
Whats math.random?
Why should i get my indentations right when the program i use shows me where they are.
_________________
|
|
Back to top |
|
|
DeltaFlyer Grandmaster Cheater Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Mon Mar 19, 2007 5:01 pm Post subject: |
|
|
ravicus wrote: | Whats math.random?
|
Look it up: http://java.sun.com/j2se/1.4.2/docs/api/
Quote: | Why should i get my indentations right when the program i use shows me where they are.
|
Because you're posting this on the internet for OTHER people to read? Make your programs readable with the PROPER indentations. A simple F2 does it in Ready to Program:
Code: |
//Imports
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;
public class diceroller
{
public static void main (String[] args)
{
//Declarations
int die1 = 0;
int die2 = 0;
int mynumber = 0;
int timesrolled = 0;
Random randomizer = new Random ();
Scanner s = new Scanner (System.in);
//Begin Main Code
//Ask the user for a number
out.print ("Please enter a number 2-12 for the two dice to roll: ");
mynumber = (s.nextInt ());
//User error checking loop
while ((mynumber) > 12 || (mynumber) < 2)
{
out.println ("You entered an invalid number.");
out.print ("Please enter a number 2-12 for the two dice to roll: ");
mynumber = (s.nextInt ());
}
out.println ("Thank-you, let the dice rolling begin!");
//Start the dice rolling loop
while ((die1) + (die2) != (mynumber))
{
die1 = randomizer.nextInt (6) + 1;
die2 = randomizer.nextInt (6) + 1;
out.print (die1);
out.print (" ");
out.println (die2);
timesrolled++;
}
//Ending Message
out.print ("Successfully rolled ");
out.print (mynumber);
out.print (" in ");
out.print (timesrolled);
out.print (" rolls");
}
}
|
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
Back to top |
|
|
ravicus Master Cheater Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Mon Mar 19, 2007 7:12 pm Post subject: |
|
|
Ready to program costs money...
_________________
|
|
Back to top |
|
|
DeltaFlyer Grandmaster Cheater Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Mon Mar 19, 2007 7:35 pm Post subject: |
|
|
ravicus wrote: | Ready to program costs money... |
I would find the link and post it if I could, but I downloaded it from an unlinked page on my school's website a year ago and my teacher removed the link from our messaging system.
Plus, I'm pretty sure that other programs have similar functions, just well hidden as they TRY to make it automatic. For example, the format shortcut in VS2005 is Ctrl + K + F.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
Back to top |
|
|
ravicus Master Cheater Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Mon Mar 19, 2007 8:07 pm Post subject: |
|
|
Ok, I'll look for it in JCreator.
_________________
|
|
Back to top |
|
|
|