import java.io.IOException;
import java.util.*;
public class CoinToss {
public enum Coins
{
HEAD, TAIL;
private static Random rand = new Random();
public static Coins flip(){
return rand.nextBoolean() ? HEAD: TAIL;
}
}
public static void main(String []args) throws IOException
{
char choice;
int heads = 0, tails = 0;
do{
System.out.println("TOSS COIN? Press y for yes.");
choice = (char)System.in.read();
if(choice == 'y' || choice == 'Y')
{
Coins c = Coins.flip();
System.out.println(c);
if(c == Coins.HEAD)
heads++;
else
tails++;
}
}while(choice == 'y' || choice == 'Y');
System.out.println("FREQUENCY:\nHeads: " + heads + "\nTails: " + tails);
}
}
Can't loop
Page 1 of 113 Replies - 370 Views - Last Post: 10 March 2012 - 12:42 PM
#1
Can't loop
Posted 10 March 2012 - 01:09 AM
Replies To: Can't loop
#2
Re: Can't loop
Posted 10 March 2012 - 01:25 AM
Be a little more descriptive please.
Jack
#3
Re: Can't loop
Posted 10 March 2012 - 01:25 AM
#4
Re: Can't loop
Posted 10 March 2012 - 01:28 AM
do{
System.out.print("TOSS COIN? Press y for yes.");
choice = (char)System.in.read();
if(choice == 'y' || choice == 'Y')
{
Coins c = Coins.flip();
System.out.println(c);
if(c == Coins.HEAD)
heads++;
else
tails++;
}
}while(choice == 'y' || choice == 'Y');
#5
Re: Can't loop
Posted 10 March 2012 - 01:32 AM
#6
Re: Can't loop
Posted 10 March 2012 - 01:39 AM
I mean to say, calue of choice is still while after first loop. so it's supposed to continue looping.
*value of choice is still y after first loop so it should continue looping
#7
Re: Can't loop
Posted 10 March 2012 - 01:46 AM
Does it end with error or anything? Does the program show the last print statement(Frequency statement)?
#8
Re: Can't loop
Posted 10 March 2012 - 01:57 AM
#9
Re: Can't loop
Posted 10 March 2012 - 02:01 AM
You mean it doesn't allow you to type input again?
Did you even try input another 'y'?
#10
Re: Can't loop
Posted 10 March 2012 - 02:15 AM
do{
...
}while(choice == 'y' || choice == 'Y');
System.out.println("\nFREQUENCY:\nHeads: " + heads + "\nTails: " + tails); //this part directly executes without letting me input another value for choice.
So basically, after just inputting 'y' once, I get the following output:
TOSS COIN? Press y for yes.y TAIL TOSS COIN? Press y for yes. FREQUENCY: Heads: 0 Tails: 1
#11
Re: Can't loop
Posted 10 March 2012 - 02:43 AM
So try this method
http://www.devdaily....edu/pj/pj010005
This should work
#12
Re: Can't loop
Posted 10 March 2012 - 03:30 AM
Here's a link to a great guide from right here on DIC: Exception Basics
Cheers!
Jack
#13
Re: Can't loop
Posted 10 March 2012 - 07:22 AM
What's this:
return rand.nextBoolean() ? HEAD: TAIL;
I've never seen anything written out like that before. How exactly does that work - and what's it called?
#14
Re: Can't loop
Posted 10 March 2012 - 12:42 PM
NantucketSleighride, on 10 March 2012 - 02:22 PM, said:
What's this:
return rand.nextBoolean() ? HEAD: TAIL;
I've never seen anything written out like that before. How exactly does that work - and what's it called?
That is called a conditional operator. I rarely see it get used or use it myself but this is how it works.
The first operand - boolean - is a boolean variable or expression.
First this boolean operand is evaluated. If it is true then the second operator evaluated and x is set to that value.
If the boolean operator is false, then the third operand is evaluated and x is set to that value.
|
|

New Topic/Question
Reply



MultiQuote






|