New to the complmunity, need some assistance please.
Okay, first off, hello to all. I am a mechanical engr. student, and am in a intro Java class. This is my first experience with programming of any sort, so I really have no idea what I am doing. Alot of monkey see, monkey do right now. I have a program, coin toss, which I am sure alot of you have seen. I have the bulk of the program functional, just the final portion, where it tallies the heads and tails, and calculates a percentage for both. I have copied and pasted, (the attach file wasn't working for me) what I have got so far, can someone please take a look and tell me (in a very slow, childlike manner) what I am doing wrong? i have highlighted in red what I think the problem areas are, but I can't figure out how to fix it. Thank you.
Nick the New Guy.
import java.util.Scanner;
public class CoinToss
{
public static void main (String [] args)
{
int headCount = 0;
int tailCount = 0;
float percentHeads = (headCount++) / 8 * 100;
float percentTails = (tailCount++) / 8 * 100;
char tossResult;
System.out.println ("Flip a coin a total of eight times.");
System.out.println ("After each flip, enter the value of the flip,");
System.out.println ("use 'h' for heads,");
System.out.println ("and use 't' for tails.");
System.out.println ("The program will keep track of the number of 'Heads',");
System.out.println ("the number of 'Tails', and the percentages of each.");
System.out.println ("What is the first value?");
Scanner keyboard = new Scanner (System.in);
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("What is the second value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("What is the third value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("What is the fourth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("What is the fifth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("What is the sixth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("What is the seventh value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("What is the eigth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h');
headCount++;
if(tossResult == 't');
tailCount++;
System.out.println ("The total amount of 'Heads' entered is:");
System.out.println ("headCount + " headCount++");
System.out.println ("The total amount of 'Tails' entered is:");
System.out.println ("tailCount + " tailCount++");
System.out.println ("The percentage of 'Heads' is:");
System.out.println ("percentHeads + percentHeads");
System.out.println ("The percentage of 'Tails' is:");
System.out.println ("percentTails");
}
}
Re: New to the complmunity, need some assistance please.
System.out.println ("headCount + " headCount++");
System.out.println ("tailCount + " tailCount++");
That will confuse it as you have an unclosed pair of " ".
I'm not sure if you want the "+" in the quotes or not.
Also, percent heads isn't being updated every time you increment percent heads. Same for percent tails.
Also, maybe you initialize percent heads and percent tails after you've got your final head and tail count.
Right now it's going to be the same value every time no matter what is chosen.
float percentHeads = (headCount++) / 8 * 100;
float percentTails = (tailCount++) / 8 * 100;
Since that appears to be the postincrement operator, I think it applies it to the original value and then increments. Also, not quite sure on this one, but if Java evaluates expressions in the normal mathematical precedence order, which I think it does, it'll be doing this:
(headCount) / (8*100)
0/800 = 0;
Re: New to the complmunity, need some assistance please.
When your printing out values such as headCount and percentHeads you dont need quotation marks.
You just need the value in the brackets such as:
System.out.println ("The total amount of 'Heads' entered is:");
System.out.println (headCount); <--------- just the value headCount, no need for the quotes.
alternatively if you wanted it on one line you could print it out by doing this:
System.out.println ("The total amount of 'Heads' entered is: " +headCount);
But for now try this for the code youve highlighted in red at the bottom:
System.out.println ("The total amount of 'Heads' entered is:");
System.out.println (headCount);
System.out.println ("The total amount of 'Tails' entered is:");
System.out.println (tailCount);
System.out.println ("The percentage of 'Heads' is:");
System.out.println (percentHeads);
System.out.println ("The percentage of 'Tails' is:");
System.out.println (percentTails);
Re: New to the complmunity, need some assistance please.
Also where you calculate the percentage here:
float percentHeads = (headCount++) / 8 * 100;
float percentTails = (tailCount++) / 8 * 100;
you dont want to divide headcount++ and tailCount++ , these are not the real values you want to work with, headCount++ adds 1 to headCount and gives headCount the new value, therefore you want to use headCount and tailCount for your percentage calculations. ie. this:
float percentHeads = (headCount) / 8 * 100;
float percentTails = (tailCount) / 8 * 100;
Re: New to the complmunity, need some assistance please.
Hmmmmmmmm something is still not right it gives the following results
The total amount of 'Heads' entered is:
8
The total amount of 'Tails' entered is:
8
The percentage of 'Heads' is:
0
The percentage of 'Tails' is:
0
Re: New to the complmunity, need some assistance please.
Also, you don't always have to use a String per se for the println.
System.out.println("The amount of 'Heads' entered is: " + headcount);
Re: New to the complmunity, need some assistance please.
First, please use the code tags to wrap your code in...it makes it readable and easy to look over.
Second, while other members seem to have the ability to guess quite efficiently, myself...I have no idea what the problem is because you haven't told me. Does this compile? Does it misbehave? Are there exceptions? The more information the better so we don't guess as to what the problem is (and in the process, waste time not solving the underlying problem)...the above posts exemplify this, as one post suggests the problem is compile time, while another a runtime problem.
Re: New to the complmunity, need some assistance please.
Also, don't have ; after your if statements. That is basically causing your if to execute an empty statement and then always will therefore increment headcount and tailcount. That's why it's always 8 for both.
";" is actually a valid statement, albeit an empty one.
Re: New to the complmunity, need some assistance please.
Yes, also like the poster above said, the calculations are doing 8*100 first.
Maybe you should try changing the maths around and do: (headcount) * 100 / 8
That should give you the correct result as your basically multiplying the count by 12.5 which is an 1/8 of 100
Re: New to the complmunity, need some assistance please.
great stuff guys, I appreciate it. Here's where I am now...
;'s deleted from if statments
upon compiling, headcount and tailcount are functional.
tried changing the math around for the percentage, still coming up with 0.0 for both heads and tails.
Code :
import java.util.Scanner;
public class CoinToss
{
public static void main (String [] args)
{
int headCount = 0;
int tailCount = 0;
float percentHeads = (headCount) * 100 / 8;
float percentTails = (tailCount) * 100 / 8;
char tossResult;
System.out.println ("Flip a coin a total of eight times.");
System.out.println ("After each flip, enter the value of the flip,");
System.out.println ("use 'h' for heads,");
System.out.println ("and use 't' for tails.");
System.out.println ("The program will keep track of the number of 'Heads',");
System.out.println ("the number of 'Tails', and the percentages of each.");
System.out.println ("What is the first value?");
Scanner keyboard = new Scanner (System.in);
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("What is the second value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("What is the third value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("What is the fourth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("What is the fifth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("What is the sixth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("What is the seventh value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("What is the eigth value?");
tossResult = keyboard.next().charAt(0);
if(tossResult == 'h')
headCount++;
if(tossResult == 't')
tailCount++;
System.out.println ("The total amount of 'Heads' entered is:");
System.out.println (headCount);
System.out.println ("The total amount of 'Tails' entered is:");
System.out.println (tailCount);
System.out.println ("The percentage of 'Heads' is:");
System.out.println (percentHeads);
System.out.println ("The percentage of 'Tails' is:");
System.out.println (percentTails);
}
}
Re: New to the complmunity, need some assistance please.
Got it, the float percentHeads / Tails is a postincrement operator. I didn't realize where you initialize the operator made a difference. Thanks so much for the help. Assignment is past due already so no credit, but atleast I learned something.
Re: New to the complmunity, need some assistance please.
How did you sort this?
Im trying to learn aswell.
Re: New to the complmunity, need some assistance please.
Quote:
Originally Posted by
djl1990
How did you sort this?
Im trying to learn aswell.
I don't want to put up the finished code, as this is a common introduction class code (I think)because I don't know if it violates any home assistance policies help by the website. Admins, please let me know if this is the case or not. The reason the averaging wasn't working was because I initialized the that operator (float percentHeads and float percentTails) before the increments for the averages were established. Simply moving them down to after the user inputs fixed that. I also had thrown in some random quote signs in the system.out.println for the headCount and tailCount that were confusing it.
I know I am being abstract but like I said, this site is a great resource, I learned more in 8 responses here than I did in two weeks in class, and I don't want to jeopardize my membership. Are you having a specific problem with a similar program or just trying to learn in general? If its specific, throw it up in this thread, don't worry about thread jacking, you'll get you're answer. I worked for two weeks on my own, came here, got enough info to figure out my answer in 2 hours.