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 17 of 17

Thread: Confused without a teacher on a loop.

  1. #1
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Confused without a teacher on a loop.

    OK, so here is the code I have so far. The Main class where I have the problem it is looping once the round is over and the balance is added from the winning tosses. I can't figure out the loop runs infinite because the balance4 is reset after each round of the loop. But I cannot figure out how to keep the balance from resetting back to zero it needs to keep going until it is equal to or greater then 1.

    import java.util.Random;
     
    public class Coin {
     
    	//fields//
     
    	private int face;
    	String sideUp;
    	double winnings;
     
     
       	//constructor//
     
    	public Coin(){
     
    	}
     
     
    	/* METHODS
    	/
    	/Toss will simulate toss of coin
    	*/
     
    	public void toss()
    	{
    		face = (int) (Math.random() * 2) + 1;
    	}
     
     
    	/*
    	/getSideUP will set value of face side of coin
    	*/
     
    	public String getSideUp()
    	{
    		if (face == 1)
    		{
    			sideUp = "Heads";
    			winnings = 1;
    		}
     
    		else
    		{
    			sideUp = "Tails";
    			winnings = 0;
    		}
    		return sideUp;
    	}
     
    	public double getResults(double coins)
    	{
    		double balance;
     
    		balance = winnings * coins;
     
    		return balance;
    	}
     
    	}

    and the Main class where I have the problem is looping once the round is over and the balance is added from the winning tosses. I can't figure out how I should make it loop. I know it only runs once but shouldn't it continue because the value of balance4 is still less that 1 or is it inheriting the int cast and rounding to 1 which would stop the loop?

     
    //************************************************************************************
    // Main.java Author: Robert Gonzalez
    //
    // 3 coin game where money won from heads side up flip coin value is added to winnings
    //************************************************************************************
    import java.text.DecimalFormat;
     
    public class Main {
     
     
    	public static void main(String[] args)
    	{
     
    		Coin nickel = new Coin();		// creates nickel coin instance
    		Coin dime = new Coin();			// creates dime coin instance
    		Coin quarter = new Coin();		// creates quarter coin instance
    		DecimalFormat formatter = new DecimalFormat("#.00");
    		double balance1, balance2, balance3,balance4 = 0;;
     
    		/*
    		/Starts the game by flipping the nickel and displaying face side.
    		*/
     
     
     
    		System.out.println("3 coin toss game");
    		System.out.println("Get exactly one dollar and win!");
    		System.out.println("\n");
     
     
     
     
    		do
    		{
    		nickel.toss();
    		System.out.print("Nickel toss shows: " + nickel.getSideUp() + "\n");
    		balance1 = nickel.getResults(.05);
     
     
    		dime.toss();
    		System.out.print("Dime toss shows: " + dime.getSideUp() + "\n");
    		balance2 = dime.getResults(.1);
     
    		quarter.toss();
    		System.out.print("Quarter toss shows: " + quarter.getSideUp() + "\n");
    		balance3 = quarter.getResults(.25);
     
    		balance4 = balance1 + balance2 + balance3;
     
    		System.out.println("You won: $ " + formatter.format(balance4) + " this round. \n");
     
    		} while (balance4 >= 1);
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Confused without a teacher on a loop.

    I wouldn't waste time complaining about your teacher. Many people here and in general taught themselves how to program, without the aid of a teacher, book, fellow students, tutoring center, or any of the other perks that being enrolled in a class entails. Honestly, I sort-of lost your question in the wall of text, so next time just cut to the chase and ask a technical question, that's what we can help with.

    I would suggest stepping through this with a debugger to see what's going on. At the very least, you should be adding more print statements: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Confused without a teacher on a loop.

    because the value of balance4 is still less that 1
    } while (balance4 >= 1);
    That while statement will loop if: balance4 >= 1 (Greater or equal to 1)

    What is the value of balance4 at the end of the loop?
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (January 30th, 2013)

  5. #4
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default

    The balance 4 is initially zero then once the loop has completed once it will equal at the most .4 depending on coins face after toss. My condition is wrong I want it to run until balance4 is equal or over 1. But I if I switch to balance4 <=1 it turns to infinite loop cuz balance4 never keeps value after running loop.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Confused without a teacher on a loop.

    I want it to run until balance4 is equal or over 1.
    Then make it loop while balance4 is less than 1. When it becomes equal or over 1 the loop should exit.

    balance4 never keeps value after running loop.
    Please explain how balance4 loses it value after running the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (January 30th, 2013)

  8. #6
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default

    I don't know how this is why I get confused. When I run it the way the statement is now program loops once if I switch it it goes into infinite loop that is what leads me to believe that my problem is the value of balance 4 is not being added after each loop to previous loops balance so that it can finally stop the loop

  9. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Confused without a teacher on a loop.

    Like I said, you should step through this with a debugger to fully understand what's going on. When are you setting the value of balance4? What is the value before you set it? What is its value after you set it? Why? You could also use print statements to figure this out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Confused without a teacher on a loop.

    What is printed out as the value of balance4 as the program loops?
    Does the print out show that the value of balance4 changes each time through the loop?
    If the value of balance4 doesn't change in the loop, then the loop exit condition will never be met and the loop could go forever.


    What is the maximum value of balance4 that you see?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (January 30th, 2013)

  12. #9
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default

    In the infinite loop the balance4 displays the rounds winnings so each round the max value of balance is .4 which should make it loop but it doesn't. I tried going back and changing all doubles to int and it worked but it does an extra loop after balance reached condition of over or equal to100. This is why I thought that it was rounding the balance and stopping the loop when I used double for the that field.

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Confused without a teacher on a loop.

    balance is .4 which should make it loop but it doesn't
    What is the condition in the while() statement that you think a value of .4 should make it loop again?
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (January 30th, 2013)

  15. #11
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default

    Do {}while(balance4 >=1) loops once println of balance4 .15
    Do {}while(balance4 <=1) infinite loop each println showed a balance4 of less than .4
    I know this problem lies with balance4 field and its something simple just can't wrap my head around it

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Confused without a teacher on a loop.

    What is the max value of balance4 that you have ever seen?
    Why do you think its value will ever be equal to 1.0?

    Try testing the code by adding these statements before the do:
         int lpCnt = 0;     //<<<<<<<<<<< debug stuff
          double maxVal = 0.0;
          int MaxLps = 500;    //  Number of loops to make
    and these at the end of the while loop, replacing the while:
             if(balance4 > maxVal)  
                 maxVal = balance4; //  save max so far
     
             if(++lpCnt > MaxLps)  break; //<<<<<<<<<< EXIT
     
        } while (balance4   < 1);
     
         System.out.println("max="+maxVal +" after " + lpCnt);

    That will show you the max value ever seen
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (January 30th, 2013)

  18. #13
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Confused without a teacher on a loop.

    Will do as soon as soon as I get out of work @ 6 and post results thank you for your help I was thinking of creating a seperate object in the coin class to store value of each loop and use that in the while condition bit this project has to be done without the use of an array.

  19. #14
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Confused without a teacher on a loop.

    So this is what it spit out after putting in that bit of code:
    max=.4 after 501

  20. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Confused without a teacher on a loop.

    That would say that balance4 will probably never be greater than .4

    What is the while() loop supposed to do? The tested value of balance4 will have to be <= .4 for the loop to ever end.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (January 30th, 2013)

  22. #16
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Confused without a teacher on a loop.

    OK, so I ended up creating a new class to hold the value of the balance and to do the summation of the balance at the end of each round and also ended up changing up all doubles to int. So my new quest is how can I combine the Coin class and the Total class into to one java document? And here is the new code if you have any pointers on cleaning it up I would appreciate it.

    //************************************************************************************
    // Main.java Author: Robert Gonzalez
    //
    // 3 coin game where money won from heads side up flip coin value is added to winnings
    //************************************************************************************
     
     
    public class Main {
     
     
    	public static void main(String[] args)
    	{
     
    		Coin nickel = new Coin();		// creates nickel coin instance
    		Coin dime = new Coin();			// creates dime coin instance
    		Coin quarter = new Coin();		// creates quarter coin instance
    		Total balance = new Total();		// creates balance total instance
     
    		int total;
     
    		/*
    		/Starts the game by flipping the nickel and displaying face side.
    		*/
     
     
     
    		System.out.println("3 coin toss game");
    		System.out.println("Get exactly one dollar and win!");
    		System.out.println("\n");
     
     
    		int x = 0;
    		do
    		{
     
     
    		nickel.toss();
    		System.out.print("Nickel toss shows: " + nickel.getSideUp() + "\n");
    		balance.sum(nickel.getResults(5));
     
     
    		dime.toss();
    		System.out.print("Dime toss shows: " + dime.getSideUp() + "\n");
    		balance.sum(dime.getResults(10));
     
    		quarter.toss();
    		System.out.print("Quarter toss shows: " + quarter.getSideUp() + "\n");
    		balance.sum(quarter.getResults(25));
     
    		x = balance.sum(0);
     
     
    		System.out.println("You've won " + x + " cents total.\n");
     
    		}while (x < 100);
     
    		if (x == 100)
    		{
    			System.out.println("You Win!!!");
    		}
    		else
    		{
    			System.out.println("Sorry You Lose.");
    		}
        	}
    }

    Coin Class
    import java.util.Random;
     
    public class Coin {
     
    	//fields//
     
    	private int face;
    	String sideUp;
    	int winnings;
     
     
       	//constructor//
     
    	public Coin(){
     
    	}
     
     
    	/* METHODS
    	/
    	/Toss will simulate toss of coin
    	*/
     
    	public void toss()
    	{
    		face = (int) (Math.random() * 2) + 1;
    	}
     
     
    	/*
    	/getSideUP will set value of face side of coin
    	*/
     
    	public String getSideUp()
    	{
    		if (face == 1)
    		{
    			sideUp = "Heads";
    			winnings = 1;
    		}
     
    		else
    		{
    			sideUp = "Tails";
    			winnings = 0;
    		}
    		return sideUp;
    	}
     
    	public int getResults(int coins)
    	{
    		int balance;
     
    		balance = winnings * coins;
     
    		return balance;
    	}
    }

    Total class
    public class Total
    	{
    		int balance2 = 0;
     
    		public int sum(int money)
    		{
     
    			balance2 += money;
     
    			return balance2;
    		}
    	}

  23. #17
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Confused without a teacher on a loop.

    Thank you Norm for your help I finally figured it out I just had to take the public off of the total class and add it to the coin class java document. I've cleaned it up some and posting it below. Thank you for your patience with all the noob questions. Lot of reading, testing and googling. If you see anything I can clean up please message me I gonna close this up as solved.

    //************************************************************************************
    // Main.java Author: Robert Gonzalez
    //
    // 3 coin game where money won from heads side up flip coin value is added to winnings
    //************************************************************************************
     
     
    public class Main {
     
     
    	public static void main(String[] args)
    	{
     
    		Coin nickel = new Coin();		// creates nickel coin instance
    		Coin dime = new Coin();			// creates dime coin instance
    		Coin quarter = new Coin();		// creates quarter coin instance
    		Total balance = new Total();	// creates balance total instance that will hold winnings.
     
     
    		/*
    		/Prints out the name and rules of the game.
    		*/
     
    		System.out.println("3 coin toss game");
    		System.out.println("Flip all 3 coins Heads you win coin value");
    		System.out.println("Tails you win nothing.");
    		System.out.println("Flip until you reach or go over a dollar");
    		System.out.println("Get exactly one dollar and win!");
    		System.out.println("\n");
     
    		int totalWon = 0;  // starts the game initializes the balance to 0.
     
    		do
    		{
    		//The nickel coin is tossed
    		nickel.toss();
     
    		//Gets the face side up value and prints to screen.
    		System.out.print("Nickel toss shows: " + nickel.getSideUp() + "\n");
     
    		//Sets amount of coin and if toss is won banks value into the balance.
    		balance.sum(nickel.getResults(5));
     
    		//The dime coin is tossed.
    		dime.toss();
     
    		//Gets the face side up value and prints to screen.
    		System.out.print("Dime toss shows: " + dime.getSideUp() + "\n");
     
    		//Sets amount of coin and if toss is won banks value into the balance.
    		balance.sum(dime.getResults(10));
     
    		//The quarter coin is tossed.
    		quarter.toss();
     
    		//Gets the face side up value and prints to screen.
    		System.out.print("Quarter toss shows: " + quarter.getSideUp() + "\n");
     
    		//Sets amount of coin and if toss is won banks value into the balance.
    		balance.sum(quarter.getResults(25));
     
    		//Returns total balance won.
    		totalWon = balance.sum(0);
     
    		System.out.println("You've won a total of " + totalWon + " cents.\n");
     
    		}while (totalWon < 100); //Continues games until balance has reached over 1 dollar.
     
    		if (totalWon == 100)
    		{
    			//Prints out winning statement to screen.
    			System.out.println("Your total was exactly 1 dollar.");
    			System.out.println("You Win!!!");
    		}
    		else
    		{
    			//Prints out losing statement to screen.
    			System.out.println("Your total was over 1 dollar.");
    			System.out.println("Sorry You Lose .");
    		}
        	}
    	}
    import java.util.Random;
     
    public class Coin {
     
    	//fields//
     
    	private int face;
    	String sideUp;
    	int balance;
     
       	//constructor//
     
    	public Coin(){
     
    	}
     
     
    	/* METHODS
    	/
    	/Toss will simulate toss of coin
    	*/
     
    	public void toss()
    	{
    		face = (int) (Math.random() * 2) + 1;
    	}
     
     
    	/*
    	/getSideUP will set value of face side of coin
    	*/
     
    	public String getSideUp()
    	{
    		if (face == 1)
    		{
    			sideUp = "Heads";
    			balance = 1;
    		}
     
    		else
    		{
    			sideUp = "Tails";
    			balance = 0;
    		}
    		return sideUp;
    	}
     
    	/* getResults sends back amount won from coin toss. */
     
    	public int getResults(int won)
    	{
    		int winnings;
     
    		winnings = balance * won;
     
    		return winnings;
    	}
    }
     
    /* The Total class creates an account that banks amounts from the winning tosses and returns that amount. */
     
    class Total
    	{
    		int balance2 = 0;
     
    		public int sum(int money)
    		{
     
    			balance2 += money;
     
    			return balance2;
     
    		}
     
    	}

Similar Threads

  1. How do I expand this code? I have no idea what my teacher wants.
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 3rd, 2012, 10:18 PM
  2. Looking for A Java Teacher
    By BigBoi2010 in forum Object Oriented Programming
    Replies: 3
    Last Post: June 7th, 2012, 10:43 AM
  3. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 07:45 AM
  4. confused on loop
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2010, 03:26 PM