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

Thread: While Statements/Boolean Based Program Help

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default While Statements/Boolean Based Program Help

    Hi, i'm new to coding with Java so keep that in mind. To practice I thought it would be a fun idea to create a super simple football simulator through the console that would just do a coin toss, then randomly decide if its going to be a Turnover (50%), and Field Goal (25%), or a Touch Down (25%). Then just to end it for now I made it so the game would stop when a team reaches 50 points or higher. Here is the code... (I use Eclipse IDE btw)

    public class Simulator {
     
    	public static void main(String[] args) {
    		int touchDown = 7;
    		int fieldGoal = 3;
    		int homeScore = 0;
    		int awayScore = 0;
    		String total = "Home: " + homeScore + " Away: " + awayScore;
    		boolean homeBall = false;
    		boolean awayBall = false;
    		double coinToss = Math.random();
     
    		if (coinToss <= 0.5){
    			homeBall = true;
    			System.out.println("The home team won the coin toss!");
    		}else{
    			awayBall = true;
    			System.out.println("The away team won the coin toss!");
    		}
     
    		if (homeScore >= 50){
    			homeBall = false;
    			awayBall = false;
    			System.out.println("Final Score: Home " + homeScore + ", Away " + awayScore);
    		}
     
    		if (awayScore >= 50){
    			homeBall = false;
    			awayBall = false;
    			System.out.println("Final Score: Home " + homeScore + ", Away " + awayScore);
    		}
     
    		if (homeBall = true){
    			double homeChance = Math.random();
    			if (homeChance < 0.5){
    				homeBall = false;
    				awayBall = true;
    				System.out.println("Home turnover!");
    			}
    			if (0.5 <= homeChance){
    				if (homeChance < 0.75){
    					homeScore = homeScore + fieldGoal;
    					System.out.println(total);
    					homeBall = false;
    					awayBall = true;
    				}else{
    					homeScore = homeScore + touchDown;
    					System.out.println(total);
    					homeBall = false;
    					awayBall = true;
    				}
    			}
    		}
     
    		if (awayBall = true){
    			double awayChance = Math.random();
    			if (awayChance < 0.5){
    				homeBall = true;
    				awayBall = false;
    				System.out.println("Away turnover!");
    			}
    			if (0.5 <= awayChance){
    				if (awayChance < 0.75){
    					awayScore = awayScore + fieldGoal;
    					System.out.println(total);
    					homeBall = true;
    					awayBall = false;
    				}else{
    					awayScore = awayScore + touchDown;
    					System.out.println(total);
    					homeBall = true;
    					awayBall = false;
    				}
    			}
    		}
     
    	}
     
    }

    The problem is that it always ends up stopping basically right after the first possession and its almost always a turnover too. I'm new to while/do statements so that might be related to the solution. I would appreciate anyone's help!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,708
    My Mood
    Mellow
    Thanks
    230
    Thanked 704 Times in 690 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    You mention being new to looping, and that's exactly what you need here. Right now your program runs from top to bottom or from start to finish without repeating any of the code between. Repeating code is done with some kind of loop. I recommend you read about controlling program flow.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    Thanks for the reply Greg. I've been researching about loops and I still cant seem to find out what's wrong with my code. Do I need to put the last two big loops at the beginning of the program?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,708
    My Mood
    Mellow
    Thanks
    230
    Thanked 704 Times in 690 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    I've been researching about loops . . .
    Then you probably know that the typical loops are for loops, while loops, and do/while loops.
    . . . Do I need to put the last two big loops . . .
    What "last two big loops?" I don't see a for, while, or do/while loop anywhere in your code.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    Would you recommend a for loop? Because when I try a while loop it just goes insane forever.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,708
    My Mood
    Mellow
    Thanks
    230
    Thanked 704 Times in 690 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    While one type of loop might be more appropriate in a given situation than the others, ALL can be written to be interchangeable. Show what you tried and describe the results. Show the whole program in its runnable form, even though it goes insane forever (an infinite loop).

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    public class Simulator {
     
    	public static void main(String[] args) {
    		int touchDown = 7;
    		int fieldGoal = 3;
    		int homeScore = 0;
    		int awayScore = 0;
    		String total = "Home: " + homeScore + " Away: " + awayScore;
    		boolean homeBall = true;
    		boolean awayBall = false;
     
    		while (homeBall = true){
    			double homeChance = Math.random();
    			if (homeChance < 0.5){
    				homeBall = false;
    				awayBall = true;
    				System.out.println("Home turnover!");
    			}
    			else if (0.5 <= homeChance){
    				if (homeChance < 0.75){
    					homeScore = homeScore + fieldGoal;
    					System.out.println(total);
    					homeBall = false;
    					awayBall = true;
    				}else{
    					homeScore = homeScore + touchDown;
    					System.out.println(total);
    					homeBall = false;
    					awayBall = true;
    				}
    			}
    		}
     
    		while (awayBall = true){
    			double awayChance = Math.random();
    			if (awayChance < 0.5){
    				homeBall = true;
    				awayBall = false;
    				System.out.println("Away turnover!");
    			}
    			else if (0.5 <= awayChance){
    				if (awayChance < 0.75){
    					awayScore = awayScore + fieldGoal;
    					System.out.println(total);
    					homeBall = true;
    					awayBall = false;
    				}else{
    					awayScore = awayScore + touchDown;
    					System.out.println(total);
    					homeBall = true;
    					awayBall = false;
    				}
    			}
    		}
     
    		double coinToss = Math.random();
    		if (coinToss <= 0.5){
    			homeBall = true;
    			System.out.println("The home team won the coin toss!");
    		}else{
    			awayBall = true;
    			System.out.println("The away team won the coin toss!");
    		}
     
    		if (homeScore >= 50){
    			homeBall = false;
    			awayBall = false;
    			System.out.println("Final Score: Home " + homeScore + ", Away " + awayScore);
    		}
     
    		if (awayScore >= 50){
    			homeBall = false;
    			awayBall = false;
    			System.out.println("Final Score: Home " + homeScore + ", Away " + awayScore);
    		}
     
     
     
    	}
     
    }

    I didn't change much except move the while homeBall and the while awayBall things up. Also changed the ifs to whiles. The output of the program is always super messed up and I have no clue how it end up like this: (with if)

    Home: 0 Away: 0
    Away turnover!
    The home team won the coin toss!

    OR this over and over forever: (with while)

    Home turnover!
    Home: 0 Away: 0
    Home turnover!
    Home: 0 Away: 0
    Home: 0 Away: 0
    Home: 0 Away: 0
    Home: 0 Away: 0
    Home turnover!
    Home: 0 Away: 0
    Home: 0 Away: 0
    Home: 0 Away: 0
    Home: 0 Away: 0
    Home: 0 Away: 0
    Home turnover!
    Home turnover!
    Home turnover!
    Home turnover!
    Home turnover!
    Home: 0 Away: 0
    Home turnover!
    Home: 0 Away: 0
    Home turnover!

    If you could redo some of the code for me or something like that I would greatly appreciate it. I didn't think that this would turn out so messed up :/.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,708
    My Mood
    Mellow
    Thanks
    230
    Thanked 704 Times in 690 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    These statements can be improved on a couple levels:

    while (homeBall = true)

    '=' is the assignment operator, '==' is to compare equality. However, there's no reason to compare a boolean to true or false, simply use the boolean variable as the condition, as in:

    while ( homeBall ).

    Let me look at the rest of your code to see what else can be improved. Why don't you do the same.

    After further study:

    I think you have all of the necessary parts, but I suggest you reorder some of the parts and then wrap the parts that make up a game in another loop, something like:

    Decide coin toss. (This part is currently at the end of play - ?)

    Start Game Loop: while ( gameInProgress ):

    homeBall or awayBall loops.

    if either score >= 50, gameInProgress = false

    End Game Loop.

    Print final score.

    Further, this statement is a great idea:

    String total = "Home: " + homeScore + " Away: " + awayScore;

    but, unfortunately, it doesn't update the way you'd like it to. The String total is set when homeScore and awayScore are both zero, and it won't change. Instead, you need a method, like:
    private static String getScore()
    {
        return "Home: " + homeScore + " Away: " + awayScore;
    }
    That approach will require you to move declaration of the variables homeScore and awayScore outside of the main() method, but we can help you with that, if you don't know how.

    Move things around and add the loop as I suggested, and then we can work on printing the scores as each round ends.

  9. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    Will do.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,708
    My Mood
    Mellow
    Thanks
    230
    Thanked 704 Times in 690 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    See what I added to my previous post.

  11. #11
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    Sounds good but for your information I haven't got to methods yet so for now i'm going to try and avoid them while I can just for learning and comprehension purposes. But everything else like the game in progress loop sounds efficient. I'll start messing around with that.

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,708
    My Mood
    Mellow
    Thanks
    230
    Thanked 704 Times in 690 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    Then instead of a method, do this to print the score each time:

    System.out.println( "Home: " + homeScore + " Away: " + awayScore );

    instead of:

    System.out.println(total);

  13. #13
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    public class Simulator {
     
    	public static void main(String[] args) {
    		int touchDown = 7;
    		int fieldGoal = 3;
    		int homeScore = 0;
    		int awayScore = 0;
    		String total = "Home: " + homeScore + " Away: " + awayScore;
    		boolean homeBall = true;
    		boolean awayBall = false;
    		boolean gameInProgress = false;
     
    		//Coin Toss
     
    		double coinToss = Math.random();
    		if (coinToss <= 0.5){
    			homeBall = true;
    			System.out.println("The home team won the coin toss!");
    			gameInProgress = true;
    		}else{
    			awayBall = true;
    			System.out.println("The away team won the coin toss!");
    			gameInProgress = true;
    		}
     
    		//Game loop
     
    		while (gameInProgress){
    			//Test to end
    			if (homeScore >= 50){
    				gameInProgress = false;
    				System.out.println("Final Score: Home " + homeScore + ", Away " + awayScore);
    			}
     
    			if (awayScore >= 50){
    				gameInProgress = false;
    				System.out.println("Final Score: Home " + homeScore + ", Away " + awayScore);
    			}
    			//Game loop
    			while (homeBall){
    			double homeChance = Math.random();
    			if (homeChance < 0.5){
    				System.out.println("Home turnover!");
    				System.out.println( "Home: " + homeScore + " Away: " + awayScore );
    				homeBall = false;
    				awayBall = true;
    			}
    			else if (0.5 <= homeChance){
    				if (homeChance < 0.75){
    					homeScore = homeScore + fieldGoal;
    					System.out.println("Home field goal!");
    					System.out.println( "Home: " + homeScore + " Away: " + awayScore );
    					homeBall = false;
    					awayBall = true;
    				}else{
    					homeScore = homeScore + touchDown;
    					System.out.println("Home touch down!");
    					System.out.println( "Home: " + homeScore + " Away: " + awayScore );
    					homeBall = false;
    					awayBall = true;
    				}
    			}
    		}
     
    		while (awayBall){
    			double awayChance = Math.random();
    			if (awayChance < 0.5){
    				System.out.println("Away turnover!");
    				System.out.println( "Home: " + homeScore + " Away: " + awayScore );
    				homeBall = true;
    				awayBall = false;
    			}
    			else if (0.5 <= awayChance){
    				if (awayChance < 0.75){
    					awayScore = awayScore + fieldGoal;
    					System.out.println("Away field goal!");
    					System.out.println( "Home: " + homeScore + " Away: " + awayScore );
    					homeBall = true;
    					awayBall = false;
    				}else{
    					awayScore = awayScore + touchDown;
    					System.out.println("Away touch down!");
    					System.out.println( "Home: " + homeScore + " Away: " + awayScore );
    					homeBall = true;
    					awayBall = false;
    				}
    			}
    		}
    		}
     
    	}
     
    }

    Yes!!!!! It works perfectly! Except for the very end when at team reaches 50 they seem to just run a couple more plays for fun XD!

    Console:

    The away team won the coin toss!
    Home turnover!
    Home: 0 Away: 0
    Away turnover!
    Home: 0 Away: 0
    Home touch down!
    Home: 7 Away: 0
    Away turnover!
    Home: 7 Away: 0
    Home turnover!
    Home: 7 Away: 0
    Away turnover!
    Home: 7 Away: 0
    Home touch down!
    Home: 14 Away: 0
    Away field goal!
    Home: 14 Away: 3
    Home touch down!
    Home: 21 Away: 3
    Away turnover!
    Home: 21 Away: 3
    Home turnover!
    Home: 21 Away: 3
    Away field goal!
    Home: 21 Away: 6
    Home touch down!
    Home: 28 Away: 6
    Away turnover!
    Home: 28 Away: 6
    Home touch down!
    Home: 35 Away: 6
    Away touch down!
    Home: 35 Away: 13
    Home turnover!
    Home: 35 Away: 13
    Away turnover!
    Home: 35 Away: 13
    Home turnover!
    Home: 35 Away: 13
    Away turnover!
    Home: 35 Away: 13
    Home field goal!
    Home: 38 Away: 13
    Away touch down!
    Home: 38 Away: 20
    Home field goal!
    Home: 41 Away: 20
    Away turnover!
    Home: 41 Away: 20
    Home turnover!
    Home: 41 Away: 20
    Away turnover!
    Home: 41 Away: 20
    Home touch down!
    Home: 48 Away: 20
    Away field goal!
    Home: 48 Away: 23
    Home turnover!
    Home: 48 Away: 23
    Away turnover!
    Home: 48 Away: 23
    Home field goal!
    Home: 51 Away: 23
    Away touch down!
    Home: 51 Away: 30
    Final Score: Home 51, Away 30
    Home touch down!
    Home: 58 Away: 30
    Away touch down!
    Home: 58 Away: 37


    Is there a statement that could easily just end the program because gameInProgress = false; doesn't seem to complete the job.

  14. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,708
    My Mood
    Mellow
    Thanks
    230
    Thanked 704 Times in 690 Posts

    Default Re: [UNSOLVED] If Statements/Boolean Based Program Help

    Your test for the end of the game is in the wrong place. Shouldn't it be AFTER either team scores? Other than that, great job moving things around and following simple directions!

  15. The Following User Says Thank You to GregBrannon For This Useful Post:

    TripleChickenJumpman (January 4th, 2014)

  16. #15
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default If Statements/Boolean Based Program Help

    Yep. I fixed it everything. Works Perfectly. Thanks for the help Greg! That was my first "big" program I did on my own and it turned out well. Friend request sent. SOLVED!

Similar Threads

  1. Need help about Football Fixtures Algorithm!..
    By qwerty53 in forum Algorithms & Recursion
    Replies: 3
    Last Post: March 15th, 2012, 05:40 AM
  2. Football Betting System
    By johndingli in forum Object Oriented Programming
    Replies: 5
    Last Post: June 29th, 2011, 08:44 AM
  3. Football League Table - Idea's?
    By RSYR in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 06:43 PM
  4. Football Results
    By RSYR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2009, 06:24 PM

Tags for this Thread