Pergunta

hi i am doing a coin toss simulator for java that must be done a certain way. it must have a string for sideup to hold the string of "heads" or "tails" made by a no arg constructor, the toss method must be void and it must have a getsideup method, then we must run the coin toss 20 times and diplay the number of heads and tails... i can do it easy with none void methods and just returning the result, but getting around this void and getsideup is driving me nuts. this is what i have so far.

import java.util.Random;
public class coin {
    public static String sideUp;

    public static void toss() {
        Random rand = new Random();
        int sideup = rand.nextInt(2);
        if (sideup == 0) {
            sideUp = "heads";
        } else {
            sideUp = "tails";
        }
    }

    public static String getsideup() {
        System.out.println(sideUp);
        return sideUp;
    }

    public static void main(String[] args) {
        // coin coin = new coin();
        int hcount = 0;
        int tcount = 0;
        for (int i = 1; i <= 20; i++) {
            if (getsideup().equals("heads")) {
                hcount++;
            } else {
                tcount++;
            }
        }
        System.out.println("total heads = " + hcount + " total tails = " + tcount);
    }
}

im hoping someone can tell me what im doing wrong and put me in the right direction.

Foi útil?

Solução

You're not calling toss() at the beginning of your loop. That's required to set a value to sideUp, and required to give sideUp to change every toss.

Outras dicas

The simple fix is put toss in your for loop before the if statement but I see a lot that can be done here. First I would add a constructor for the Coin class and add hcount and tcount to the class variables and make heads and tails constants:

private String sideUp;
private int hcount;
private int tcount;
private static final String HEADS = "Heads";
private static final String Tails = "Tails";

Coin()
{
    this.sideUp = HEADS;
    this.hcount = 0;
    this.tcount = 0;
}

Then I would make a method to check the toss:

public  void checkToss()
{
    if (getsideup().equals(HEADS)) 
        hcount++;
    else
        tcount++;
}

Now add the toss() and checkCoin() method to the for loop before the if statement. It should look like this:

for (int i = 1; i <= 20; i++)
{
    coin.toss(); 
    coin.checkToss();
}

I would also make a getter for the heads count and tails count:

public int getHeadsCount()
{
    return this.hcount;
}

public int getTailsCount()
{
    return this.tcount;
}

Everything put together looks like this:

import java.util.Random;
public class Coin 
{
    private  String sideUp;
    private int hcount;
    private int tcount;
    private static final String HEADS = "Heads";
    private static final String TAILS = "Tails";

    Coin()
    {
        this.sideUp = HEADS;
        this.hcount = 0;
        this.tcount = 0;
    }

    public void toss()
    {
        Random rand = new Random();
        int sideup = rand.nextInt(2);
        if (sideup == 0) 
            sideUp = HEADS;
        else 
            sideUp = TAILS;    
    }

    public String getsideup()
    {
        System.out.println(sideUp);
        return sideUp;
    }

    public void checkToss()
    {
    if (getsideup().equals(HEADS)) 
        this.hcount++;
    else
        this.tcount++;
    }

    public int getHeadsCount()
    {
        return this.hcount;
    }

    public int getTailsCount()
    {
        return this.tcount;
    }

    public static void main(String[] args)
    {
        Coin coin = new Coin();
        for (int i = 1; i <= 20; i++) 
        {
            coin.toss();
            coin.checkToss();
        }
        System.out.println("Total Heads = " + coin.getHeadsCount() + " Total Tails = " + coin.getTailsCount());
    }
}
import java.util.Random;
public class coin
{
public static String sideUp;
public int hcount=0;
public int tcount=0;

public static void toss()
     {
        Random rand = new Random();
        int sideup = rand.nextInt(2);
        if (sideup == 0)
            {
                sideUp = "heads";
                hcount++;
            }
            else 
            {
                sideUp  = "tails";
                tcount++;
            } 
      }       

  public static void main(String[] args)
  {
    for(int i=0;i<20;i++)
    {toss();}
    System.out.println("total heads = " + hcount + " total tails = " + tcount);
   }
}
for(int i = 1; i <= 20; i++) 
    {
        toss();
      if (getsideup().equals("heads")) 
      {
        hcount++;
      } else 
      {
        tcount++;
      }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top