Results 1 to 3 of 3

Thread: Craps score

  1. #1
    lynkxs is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Craps score

    I'm having trouble with keeping the scores. Whenever I try playing again, the current score doesn't add up to the previous one.

    Java Code:
    import java.util.Scanner;
    public class Craps {
        public static void main(String[] args) {
    
            int die;
            die = 1;
            Scanner input = new Scanner(System.in);
    
            while(die != 0){
           
                System.out.print("Enter 1 to play or 0 to exit: ");
       
                die = input.nextInt();
       
                switch (die){
    
                    case 1:
                        int dice1 = (int)(Math.random()*6) + 1;
                        int dice2 = (int)(Math.random()*6) + 1;
                        Dice(dice1, dice2);
                        break;
    
                    case 0:
                        System.out.println("Game over.");
                        System.exit(0);
                        break;
                }
            }
        }
       
        public static void Dice(int dice1, int dice2){
            
            int point = 0;
            
            int total = dice1 + dice2;
    
            point += total;
            
            if (total == 2 || total == 3 || total == 12) {
                System.out.println("You rolled " + dice1 + " + " + dice2 + " = " + total);
                System.out.println("You lose!");
                System.out.println();
            }
            else if (total == 7 || total == 11) {
                System.out.println("You rolled " + dice1 + " + " + dice2 + " = " + total);
                System.out.println("Natural. You win!");
                System.out.println();
            }        
            else if (total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10) {
                System.out.println("Point: " + total);
                System.out.println("You rolled " + dice1 + " + " + dice2 + " = " + total);
                System.out.println("You win!");
                System.out.println();
            }
            
        }
    }
    Last edited by lynkxs; 02-26-2013 at 08:59 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    985
    Rep Power
    2

    Default Re: Craps

    First, put your code in tags to keep formatting and make it readable.

    Second, your point variable in method Dice (methods should actually start with a lower case letter) is reset to 0 every time the method is called. Move the variable to the class level, i.e:
    Java Code:
    class Craps {
        private static int point;
    ...
    }
    You can now access the variable from method Dice and it won't be reset every time.

  3. #3
    lynkxs is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

Similar Threads

  1. setting counter in craps game....
    By meesh in forum New To Java
    Replies: 5
    Last Post: 01-22-2013, 08:14 AM
  2. Problem with Ending Craps game
    By Ebayer in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 07:17 PM

Posting Permissions

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