Results 1 to 5 of 5
Thread: Problem with Ending Craps game
- 02-10-2011, 04:49 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Problem with Ending Craps game
Hi,
im working on a assignment from a book i bought for java.
The assigment is to write a simple version of the game Craps.
heres my problems, ive highlighted the area im having problem with.
i want the program to End if one of the 5 highlighted conditions are true.
as it is now it just continues on.
could someone please point me to the right direction?
import java.util.Scanner;
import java.util.Random;
public class MAIN {
public static void main(String[] args){
Random random = new Random();
Scanner temp = new Scanner(System.in);
int randOne = random.nextInt(6)+1;
int randTwo = random.nextInt(6)+1;
int sum = randOne + randTwo;
int points = 0;
int roll = 1;
String answer;
switch (sum) {
case 7:
System.out.println("YOU ROLLED NATURAL (7) ON YOUR FIRST ROLL!!, YOU WON!!");
roll++; break;
case 11:
System.out.println("YOU ROLLED NATURAL (11) ON YOUR FIRST ROLL!!, YOU WON!!");
roll++; break;
case 2:
System.out.println("YOU ROLLED SNAKE EYES (2) ON YOUR FIRST ROLL!!, YOU LOST!!");
roll++; break;
case 3:
System.out.println("YOU ROLLED CROSS EYES (3) ON YOUR FIRST ROLL!!, YOU LOST!!");
roll++; break;
case 12:
System.out.println("YOU ROLLED BOX CARS (12) ON YOUR FIRST ROLL!!, YOU LOST!!");
roll++; break;
case 4:
points=sum; roll++;; break;
case 5:
points=sum; roll++;; break;
case 6:
points=sum; roll++; break;
case 8:
points=sum; roll++; break;
case 9:
points=sum; roll++; break;
case 10:
points=sum; roll++; break;
default:
roll++;
}
System.out.println("SCOREBOARD");
System.out.println("DICE 1 DICE 2");
System.out.println(randOne +"\t "+ randTwo) ;
System.out.println(points);
do
{
int randOnes = random.nextInt(6)+1;
int randTwos = random.nextInt(6)+1;
int sumTwo = randOnes + randTwos;
if (sumTwo == 7)
System.out.println("You rolled 7 , YOU LOST!");
else if
(sumTwo == points)
System.out.println("Your roll was equal to points value, YOU WON!");
else
System.out.println("Type ROLL to roll again");
answer = temp.next();
}
while (answer.equalsIgnoreCase("ROLL"));
}
}
- 02-10-2011, 09:42 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 70
- Rep Power
- 0
To actually end a program, do System.exit(0);
- 02-11-2011, 12:10 AM #3
That is the cheats way out. A better more robust solution is to use boolean(s). For all those cases you set a boolean to false and only keep playing the game while the boolean is true.
- 02-11-2011, 12:14 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 70
- Rep Power
- 0
Also you probably want to do a while loop, instead of a do while, and you can add a boolean check (&& keepAlive), where keepAlive is false in those red areas.
- 02-11-2011, 07:17 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Some problem with game
By kirayamato in forum NetBeansReplies: 13Last Post: 12-01-2010, 06:06 AM -
My java game problem!? Help!!!
By Jcbconway in forum AWT / SwingReplies: 32Last Post: 09-26-2010, 09:33 PM -
ending a graphics class
By imorio in forum AWT / SwingReplies: 11Last Post: 08-16-2010, 05:41 PM -
SUDOKU game problem
By bumblyb33 in forum AWT / SwingReplies: 2Last Post: 05-05-2009, 11:18 PM -
[SOLVED] Ending for loop with a String
By jdiesel in forum New To JavaReplies: 3Last Post: 01-08-2009, 02:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks