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.
Code java:
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?
Code java:
//************************************************************************************
// 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);
}
}
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
Re: Confused without a teacher on a loop.
Quote:
because the value of balance4 is still less that 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?
Re: Confused without a teacher on a loop.
Quote:
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.
Quote:
balance4 never keeps value after running loop.
Please explain how balance4 loses it value after running the loop.
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.
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?
Re: Confused without a teacher on a loop.
Quote:
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?
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:
Code :
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:
Code :
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
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.
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
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.
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.
Code java:
//************************************************************************************
// 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
Code java:
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
Code java:
public class Total
{
int balance2 = 0;
public int sum(int money)
{
balance2 += money;
return balance2;
}
}
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.
Code java:
//************************************************************************************
// 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 .");
}
}
}
Code java:
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;
}
}