简体   繁体   English

Java投掷模拟器问题

[英]java coin toss simulator problems

hi i am doing a coin toss simulator for java that must be done a certain way. 嗨,我正在为Java做一个投币模拟器,必须以某种方式完成。 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. 它必须有一个字符串用于sideup,以容纳由无参数构造函数生成的“ heads”或“ tails”字符串,toss方法必须为空并且必须具有getsideup方法,然后我们必须掷硬币20次并改变正面和反面的数量...我可以轻松地使用无void方法并返回结果,但是绕过该void和getsideup会让我发疯。 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. 我希望有人可以告诉我我做错了什么,并将我带往正确的方向。

You're not calling toss() at the beginning of your loop. 您不是在循环开始时调用toss()。 That's required to set a value to sideUp, and required to give sideUp to change every toss. 这是将值设置为sideUp的要求,并且需要给sideUp更改每一次折腾。

The simple fix is put toss in your for loop before the if statement but I see a lot that can be done here. 简单的解决方法是在if语句之前放入for循环中,但是我发现这里可以做很多事情。 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: 首先,我将为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. 现在,在if语句之前,将toss()和checkCoin()方法添加到for循环中。 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());
    }
}

What about this ? 那这个呢 ?

import java.io.*;
import java.math.*;
class Coin_Toss {
public static void main(String args[])throws Exception {
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    double d, drop, side, s_d, re=1;
    int tries=0, score=0;
    while(re==1) {
        tries++;
        System.out.println("-> Hi, please choose your side\n-> Press 1 for    Head\n-> Press 2 for Tails");
        side=Integer.parseInt(br.readLine());
        drop=Math.random();
        d = Math.round(drop);
        if(d== 0) {
            s_d = 1;
        }
        else {
          s_d = 2;
        }
        if(s_d == side) {
            System.out.println("-> You Won");
            score++;
        }
        else {
            System.out.println("-> You Lose");
        }
        if(s_d==1) {
           System.out.println("->Its Heads");
        }
        else if(s_d==2) {
           System.out.println("-> Its tails");
        }
        System.out.println("-> Press 1 to Toss again \n -> Press 2 to     Quit");
        re=Double.parseDouble(br.readLine());
        if(re==2) {
            System.out.println("You Scored "+score+" out of "+tries);
        }
    }
}
}
for(int i = 1; i <= 20; i++) 
    {
        toss();
      if (getsideup().equals("heads")) 
      {
        hcount++;
      } else 
      {
        tcount++;
      }
    }
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);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM