Results 1 to 4 of 4
- 10-16-2011, 03:17 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
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
Thanks for the help!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); } }
-
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.
- 10-16-2011, 03:42 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: If Statement Never Gets Called (SUPER EASY!)
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.Java Code:if (Choice == "T")
-----
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.
- 10-16-2011, 03:44 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
calling variable using super super..
By Stephen Douglas in forum New To JavaReplies: 7Last Post: 08-16-2010, 06:12 AM -
Will the connection.close() and statement.close() ever be called???
By Stephen Douglas in forum New To JavaReplies: 13Last Post: 04-09-2010, 11:15 AM -
this() and super() ?
By bob in forum New To JavaReplies: 4Last Post: 08-13-2009, 04:56 PM -
super
By dj kourampies in forum New To JavaReplies: 5Last Post: 01-23-2009, 03:07 PM -
Super CSV 1.20
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-27-2007, 08:22 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks