Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: If Statement Never Gets Called (SUPER EASY!)

  1. #1
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default If Statement Never Gets Called (SUPER EASY!)

    Okay so, no matter what the results of this coin toss, The user's money never goes up.
    What i want is if the results are HHH your money should be 3 * The money specifed for this
    toss.

    So:
    Example:

    How many coin tosses would you like to make 1-50?
    20
    20 Tosses will be made
    ---------------------------------
    How much money is each toss worth in $?
    2
    Each toss will be worth 2$
    ---------------------------------
    Do you want Heads(H) or Tails(T)
    H
    H Has been chosen
    ---------------------------------
    Here are the results:
    H H H T T T T T T H H T T T H T T H H H
    9 Heads and 11 Tails
    Your money is 18


    Is all I want.
    But every single time it always says your money is 0 :(
    here's my code
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    public class Coing_Toss {
    	public static void main (String args[]){
    		String  CpuChoice = null;
    		int Hcount = 0;
    		int Tcount = 0;
    		int Money = 0;
    		Scanner scanner = new Scanner(System.in);
    		
    	System.out.println("How many coin tosses would you like to make 1-50?");
    		String Tosses = scanner.nextLine();
    		int ParseTosses = Integer.parseInt(Tosses);
    	System.out.println(Tosses + " Tosses will be made");
    	System.out.println("---------------------------------");
    	System.out.println("How much money is each toss worth in $?");
    		String Tossworth = scanner.nextLine();
    		int ParseTossworth = Integer.parseInt(Tossworth);
    	System.out.println("Each toss will be worth " + Tossworth + "$");
    	System.out.println("---------------------------------");
    	System.out.println("Do you want Heads(H) or Tails(T)");
    		String Choice = scanner.nextLine();
    	System.out.println(Choice + " Has been chosen");
    	System.out.println("---------------------------------");
    
    	
    	System.out.println("Here are the results:");
    
    	for(int i=1; i<(ParseTosses+1); i++)
    	{
    		 Random randomGenerator = new Random();
    		  int RandomInt = randomGenerator.nextInt(2);
    		  if (RandomInt == 1)
    				  {
    			 CpuChoice = "H";
    			 Hcount = Hcount + 1;
    			 System.out.print("H ");
    				  }
    		  		else
    		  		{
    			  CpuChoice = "T";
    			  Tcount = Tcount + 1;
    			  System.out.print("T ");
    		  		}
    		  
    		  
    	
    	}
    	
    	if (Choice == "T")
    	{
    		Money = (ParseTossworth * Tcount);
    	}
    	
    	if (Choice == "H")
    	{
    		Money = (ParseTossworth + Hcount);
    	}
    	
    	System.out.println("");
    	System.out.println(Hcount + " Heads and " + Tcount + " Tails");	  
    	System.out.println("Your money is " + Money);
    		  
    		  
    	}
    }
    Thanks for the help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statement Never Gets Called (SUPER EASY!)

    Don't compare Strings like this:

    Java Code:
    if (Choice == "T") {

    Instead you'll want to use the equals or equalsIgnoreCase method:

    Java Code:
    if (Choice.equals("T")) {

    The problem is the first code compares if one String object is the same as another, and you really don't care if this is true or not. Instead you are interested in knowing if both Strings hold the same characters in the same order which is what the equals/equalsIgnoreCase check.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: If Statement Never Gets Called (SUPER EASY!)

    Java Code:
    if (Choice == "T")
    Don't compare strings (or other objects) with ==, instead use their equals() method. The reason is that each class defines equals() in a way that makes sense for that class. For instance the String class equals() will return true when the strings are made up of the same characters in the same order: even if they are different instances of that string.

    -----

    Please use standard Java coding conventions. Variables should start with a lowerCaseLetter and camel case used where the variable is made up of multiple words. So choice, hCount etc. The more descriptive the variable, the better, so hCount might be better as headCount or numHeads. The aim is to make the code as readable (and self documenting) as possible.
    Fubarable likes this.

  4. #4
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: If Statement Never Gets Called (SUPER EASY!)

    Thanks so much!
    Works like a charm!

Similar Threads

  1. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 07:12 AM
  2. Replies: 13
    Last Post: 04-09-2010, 12:15 PM
  3. this() and super() ?
    By bob in forum New To Java
    Replies: 4
    Last Post: 08-13-2009, 05:56 PM
  4. super
    By dj kourampies in forum New To Java
    Replies: 5
    Last Post: 01-23-2009, 04:07 PM
  5. Super CSV 1.20
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •