Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: While loop problem

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default While loop problem

    I have to make a program that generates 2 random numbers and awards money if certain numbers come out (2 of the same = 3.00, 2 even numbers that aren't the same will give 50 cents, and 2 different numbers gets nothing).

    I have the main code done but I cannot make it loop to ask for the next part.
    Basically I need it to run once, update the times variable (meaning you've played X amount of times. and also I need the Amount won to change).

    I cannot make it loop and update those values.
    I think I need a return somewhere but I don't know how to implement it with a for loop.

    here is the code:
    import java.util.Scanner;
    import java.io.*;
    import java.text.NumberFormat;
     
    // 
    // 
    //
    // 
    // 
     
    public class RollEE
     
    {
     
    public static void main(String[]args)
     
    {
     
    	Scanner keyboard = new Scanner (System.in);
     
    	double balance = 10.00;
    	int randomNumber;
    	int randomNumber2;
    	int randomNumberSum;
    	int counter;
    	int times = 0;
    	double amountLost = 0;
     
    	System.out.println("*** Welcome to the RollEE program **");
    	System.out.println("A play costs 1 dollar. If you roll equal numbers, you win 3 dollars.");
    	System.out.println("If you roll even, you win 50 cents. If you roll odd, you do not win anything.");
    	System.out.print("Do you want to play? Type (Y/N): ");
    	String response = keyboard.nextLine();
     
     
    		randomNumber = (int)(Math.random( ) * 6)+1;
    		randomNumber2 = (int)(Math.random( ) * 6)+1; 
     
    		while (response.equals("y") || response.equals("Y"))
     
    	{
    		System.out.println("Thank you for your bet. A bet of $1 has been placed.");
     
    		 NumberFormat nf = NumberFormat.getInstance();
                    nf.setMinimumFractionDigits(2);
                    nf.setMaximumFractionDigits(2);
     
    		if (randomNumber == randomNumber2)
    	{
    		System.out.println("You rolled " + randomNumber + ", " +  randomNumber2 + ". " + "For this play you won $3.00");
    		balance = balance + 2.00;
    		times = times++;
    		System.out.println("Your new balance is: $" + nf.format(balance));
    	}
     
     
    		randomNumberSum = randomNumber + randomNumber2;
     
    		if (randomNumberSum%2 == 1)
    	{
    		System.out.println("You rolled " + randomNumber + ", " +  randomNumber2 + ". " + "For this play you won $0.00");
    		balance = balance - 1.00;
    		times = times++;
    		System.out.println("Your new balance is: $" + nf.format(balance));
    	}
    		if ((randomNumber != randomNumber2) && (randomNumberSum%2 != 1 ))
    	{
    		System.out.println("You rolled " + randomNumber + ", " + randomNumber2 + ". " + "For this play you won $.50");
    		balance = balance - 0.50;
    		amountLost = -0.50;
    		times = times++;
    		System.out.println("Your new balance is: $" + nf.format(balance));
    	}
    }
     
    		double amountLost2 = Math.abs(amountLost);
     
    	while ((response.equals("n") || response.equals("N")) && (amountLost < 0))
    	{
    		System.out.println("You played " + times + " times. You lost $" + amountLost2);
    		System.exit(0);
    	}
    }
     
    }
    By the way this gives an infinite loop. My other version has a System.exit(0) in it which terminates it after one go, but I only need it to terminate if the user inputs 'N' for no. Other then that it'll keep running and asking for the users input until he/she says no.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: While loop problem

    The best advice I give to n00bs is: do not shove all your code into the main method.

    Break the program down into smaller chunks and each chunk goes into its own method. For your problem you have all the game code at the top and then at the bottom you have a loop which is infinite because response never changes inside the loop. What you need to do is wrap a loop around the rest of your code. So putting those two things together your code might look like:
    main() {
        while user has not quit {
            playgame()
            get user response to play again
        }
    }
     
    playGame() {
        number1 = getRandomNumber();
        number2 = getRandomNumber();
        logic that determines if player wins or loses
    }
     
    getRandomNumber() {
        // note this is not really necessary but reinforces the use of methods
        return random number
    }
    You could even break the winning logic part down into smaller methods.

Similar Threads

  1. Problem with Loop
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 8th, 2011, 11:12 AM
  2. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 12:23 PM
  3. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 11:23 AM
  4. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 02:31 PM