java抛硬币模拟器问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22355961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:11:32  来源:igfitidea点击:

java coin toss simulator problems

javacomputer-science

提问by stev0104

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.

嗨,我正在做一个必须以某种方式完成的 java 抛硬币模拟器。它必须有一个用于 sideup 的字符串来保存由无参数构造函数生成的“正面”或“反面”字符串,toss 方法必须为 void 并且它必须有一个 getsideup 方法,然后我们必须运行 20 次抛硬币和diplay 正面和反面的数量...我可以很容易地使用无无效方法并返回结果,但是绕过这个无效和 getsideup 使我发疯。这是我到目前为止。

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.

我希望有人能告诉我我做错了什么,并让我朝着正确的方向前进。

采纳答案by NESPowerGlove

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.

您没有在循环开始时调用 toss() 。这需要为 sideUp 设置一个值,并且需要给 sideUp 以更改每次折腾。

回答by Hirak

for(int i = 1; i <= 20; i++) 
    {
        toss();
      if (getsideup().equals("heads")) 
      {
        hcount++;
      } else 
      {
        tcount++;
      }
    }

回答by javawocky

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

回答by Dom

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:

简单的修复是在 if 语句之前放入 for 循环中,但我看到这里可以做很多事情。首先,我会为 Coin 类添加一个构造函数,并将 hcount 和 tcount 添加到类变量中,并使正面和反面常量:

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:

现在将 toss() 和 checkCoin() 方法添加到 if 语句之前的 for 循环中。它应该是这样的:

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