1 Replies - 328 Views - Last Post: 10 October 2012 - 02:19 AM Rate Topic: -----

#1 yami123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-October 12

how to make a program loop while still counting

Posted 10 October 2012 - 12:27 AM

Ok so I'm trying to make this work. It's a very basic version of that game liar dice the problem I have a comment above, but basically they should only be allowed to roll again a total of 3 times but when I send the program back to the main menu it resets my loop to = 0 how do I make it keep counting while still looping back to this menu. I'm sure there are easier ways to do a lot of what I've done but I'm just getting started in java. Thanks

import java.util.*;
public class liardice {
	public static void main(String[] args) {
		//a Scanner.
		Scanner myScanner = new Scanner (System.in);
		//and possibly a variable...or two
		int answer;
		String claim;
		//these are the dice
		int dice1;
		int dice2;
		int dice3;
		int dice4;
		int dice5;
		int loop = 0;
		
		//numbers on the dice
		 Random rand = new Random(); 
		 dice1 = rand.nextInt(6) + 1;
		 dice2 = rand.nextInt(6) + 1;
		 dice3 = rand.nextInt(6) + 1;
		 dice4 = rand.nextInt(6) + 1;
		 dice5 = rand.nextInt(6) + 1;
		 
		// main menu
		System.out.println("Would you like to" + "\n enter (1) for Make a Claim" 
		+ "\n enter (2) for roll again.");
		// user enters 2.
		answer = myScanner.nextInt();
		myScanner.nextLine();
		
		//a fancy new if statement
		if(answer == 2)

		{

		// roll again
			 dice1 = rand.nextInt(6) + 1;
			 dice2 = rand.nextInt(6) + 1;
			 dice3 = rand.nextInt(6) + 1;
			 dice4 = rand.nextInt(6) + 1;
			 dice5 = rand.nextInt(6) + 1;
			 System.out.println("You rolled again.");
			 
			 //So this is where the loop counter should have come into play but the main(null)
			 //resets it to 0 every time so not sure how to fix.
			 if(loop < 3)
				 main(null);
			 if (loop == 3);
					// prompt the user for their claim and then print out their claim and show their hand.
					System.out.println("Please enter your claim:");
					claim = myScanner.nextLine();
					
					System.out.println("You claimed: " + claim);
					System.out.println("Your roll was: " +  "\t" + dice1 + "\t" + dice2 
							+ "\t" + dice3 + "\t" + dice4 + "\t" + dice5);
					
			 }

		if(answer == 1);

		{
		// asks for a claim then reveals dice
		System.out.println("Please enter your claim:");
		claim = myScanner.nextLine();
		
		System.out.println("You claimed: " + claim);
		System.out.println("Your roll was: " +  "\t" + dice1 + "\t" + dice2 
				+ "\t" + dice3 + "\t" + dice4 + "\t" + dice5);
		}
		myScanner.close();
	}

}
  


Is This A Good Question/Topic? 0
  • +

Replies To: how to make a program loop while still counting

#2 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: how to make a program loop while still counting

Posted 10 October 2012 - 02:19 AM

You will have to learn about the shiny new "while" statement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1