13 Replies - 370 Views - Last Post: 10 March 2012 - 12:42 PM Rate Topic: -----

#1 BrandNewDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 11

Can't loop

Posted 10 March 2012 - 01:09 AM

I don't know what's wrong with my code. Any help?

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);
    }

}






Is This A Good Question/Topic? 0
  • +

Replies To: Can't loop

#2 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Can't loop

Posted 10 March 2012 - 01:25 AM

Well what are you trying to do? And what's wrong with it?

Be a little more descriptive please.

Jack
Was This Post Helpful? 0
  • +
  • -

#3 noneed52  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 12

Re: Can't loop

Posted 10 March 2012 - 01:25 AM

What's your problem exactly? It doesn't loop at all?
Was This Post Helpful? 0
  • +
  • -

#4 BrandNewDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 11

Re: Can't loop

Posted 10 March 2012 - 01:28 AM

The problem is that this part of the code doesn't loop the way it's supposed to.

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');


Was This Post Helpful? 0
  • +
  • -

#5 noneed52  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 12

Re: Can't loop

Posted 10 March 2012 - 01:32 AM

Check what kind of value 'choice' holds at the end of the loop.
Was This Post Helpful? 0
  • +
  • -

#6 BrandNewDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 11

Re: Can't loop

Posted 10 March 2012 - 01:39 AM

Value of y after first loop is still while so the loop should continue but it's not.

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
Was This Post Helpful? 0
  • +
  • -

#7 noneed52  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 12

Re: Can't loop

Posted 10 March 2012 - 01:46 AM

It looks fine to me.......
Does it end with error or anything? Does the program show the last print statement(Frequency statement)?
Was This Post Helpful? 0
  • +
  • -

#8 BrandNewDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 11

Re: Can't loop

Posted 10 March 2012 - 01:57 AM

It doesn't display a compile-time error. But for instance, if the user types in y, the first loop would execute and then since the value of choice is still 'y', it's supposed to loop again. But instead, it just displays "TOSS COIN? Press y for yes." without asking for a new value of choice.
Was This Post Helpful? 0
  • +
  • -

#9 noneed52  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 12

Re: Can't loop

Posted 10 March 2012 - 02:01 AM

Well then it does loop....
You mean it doesn't allow you to type input again?
Did you even try input another 'y'?
Was This Post Helpful? 0
  • +
  • -

#10 BrandNewDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 11

Re: Can't loop

Posted 10 March 2012 - 02:15 AM

No I can't input a new value for choice because the statements after the do-while loop directly executes.

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


Was This Post Helpful? 0
  • +
  • -

#11 noneed52  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-March 12

Re: Can't loop

Posted 10 March 2012 - 02:43 AM

I guess System.in.read() doesn't work after the first time.
So try this method

http://www.devdaily....edu/pj/pj010005

This should work
Was This Post Helpful? 0
  • +
  • -

#12 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Can't loop

Posted 10 March 2012 - 03:30 AM

If you're gonna be throwing exceptions, you have to use try, catch, and finally.

Here's a link to a great guide from right here on DIC: Exception Basics

Cheers!

Jack
Was This Post Helpful? 0
  • +
  • -

#13 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 98
  • Joined: 13-February 11

Re: Can't loop

Posted 10 March 2012 - 07:22 AM

Aside from your question - I have a question of my own.

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?
Was This Post Helpful? 0
  • +
  • -

#14 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Can't loop

Posted 10 March 2012 - 12:42 PM

View PostNantucketSleighride, on 10 March 2012 - 02:22 PM, said:

Aside from your question - I have a question of my own.

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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1