I'm creating a game where the goal is to roll two dice and if you get a 7 or 11 you win 10 points, otherwise you lose 5. You start with 100 points. I have everything set up so far but I don't know how to keep the program going my subtracting 5 or adding 10 to the original 100.

For instance: you roll an 11 so you have 110 points. The next roll you roll an 8 so you should now have 105 points. How does one do this?

All I can do is keep the program repeating itself, so I can only get 110 points or 95. There's no subtraction or addition after the original roll.

My program so far :

import java.io.*;
import java.util.*;
public class DiceGame
{

       public static void main(String args[])
       {
         for(;;){
         Scanner kbReader=new Scanner(System.in);
         System.out.println("");
         System.out.println("Welcome to the 7-11 Dice Game.");
         System.out.println("");
         Random r=new Random();
         int randint = r.nextInt(6)+1;

         int randint1 = r.nextInt(6)+1;

         int total=randint + randint1;
         System.out.println("Random # 1 = " + randint + ".");
         System.out.println("Random # 2 = " + randint1 + ".");    


                    if(randint ==1)
                 {
                    System.out.println("\n|--------|\n|        |\n|    *   |\n|        |\n|--------|");
                 }

                 {
                    if(randint ==2)
                    System.out.println("\n|--------|\n|    *   |\n|        |\n|    *   |\n|--------|");
                 }
                 {
                    if(randint ==3)
                    System.out.println("\n|--------|\n|    *   |\n|    *   |\n|    *   |\n|--------|");
                }
                    if(randint==4)
                    System.out.println("\n|--------|\n|  *  *  |\n|        |\n|  *  *  |\n|--------|");
                {
                {
                    if(randint==5)
                    System.out.println("\n|--------|\n|  *  *  |\n|    *   |\n|  *  *  |\n|--------|");
                }
                {
                    if(randint==6)
                    System.out.println("\n|--------|\n|  *  *  |\n|  *  *  |\n|  *  *  |\n|--------|");
                }
                {
                    if(randint1==1)
                    System.out.println("\n|--------|\n|        |\n|    *   |\n|        |\n|--------|");
                }
                {
                    if(randint1==2)
                    System.out.println("\n|--------|\n|    *   |\n|        |\n|    *   |\n|--------|");
                }
                {
                    if(randint1==3)
                    System.out.println("\n|--------|\n|    *   |\n|    *   |\n|    *   |\n|--------|");
                }
                {
                    if(randint1==4)
                    System.out.println("\n|--------|\n|  *  *  |\n|        |\n|  *  *  |\n|--------|");
                }
                {
                    if(randint1==5)
                    System.out.println("\n|--------|\n|  *  *  |\n|    *   |\n|  *  *  |\n|--------|");
                }
                {
                    if(randint1==6)
                    System.out.println("\n|--------|\n|  *  *  |\n|  *  *  |\n|  *  *  |\n|--------|");
                }
                System.out.println("You rolled a " + total +".");                
                }
                int cash=100;

                if(total==7 || total==11)
                {
                cash=cash+10;
                System.out.println("You won 10 points.");
                }
                else
                {
                    cash=cash-5;
                    System.out.println("You lose 5 points.");
                }

                System.out.println("You have " + cash + " left.");

                System.out.print("Do you wish to roll again? ");
                System.out.print(" ");
                String play;
                play=kbReader.next();
                if(play.equalsIgnoreCase("yes"))
                {                   
                    continue;
                }

                if(play.equalsIgnoreCase("no"))
                {
                    System.exit(0);
                }         
                if(cash==0)
                {
                    System.exit(0);
                }
            }
       }
}

Recommended Answers

All 3 Replies

If you move your for() loop down a few lines, you can keep calling nextInt() on the Random that you have created.

If you move the declaration and initialization of 'cash' above the beginning of that loop, the result of the rolls will add/subtract from the starting value without resetting to 100.

In addition to what Ez has said, I would change your forever loop (that is your for(;;) ) to while(cash > 0) , that way when the player runs out of cash the game is over.

Thanks.

I have some other things to do but I'm sure I have it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.