Results 1 to 5 of 5
  1. #1
    Ebayer is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default 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"));



    }
    }

  2. #2
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    To actually end a program, do System.exit(0);

  3. #3
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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.

  4. #4
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    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.

  5. #5
    Ebayer is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Some problem with game
    By kirayamato in forum NetBeans
    Replies: 13
    Last Post: 12-01-2010, 06:06 AM
  2. My java game problem!? Help!!!
    By Jcbconway in forum AWT / Swing
    Replies: 32
    Last Post: 09-26-2010, 09:33 PM
  3. ending a graphics class
    By imorio in forum AWT / Swing
    Replies: 11
    Last Post: 08-16-2010, 05:41 PM
  4. SUDOKU game problem
    By bumblyb33 in forum AWT / Swing
    Replies: 2
    Last Post: 05-05-2009, 11:18 PM
  5. [SOLVED] Ending for loop with a String
    By jdiesel in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 02:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •