简体   繁体   English

如何根据用户输入进行硬币翻转模拟工作? java eclipse

[英]how can I make this coin flip simulation work based on user input? java eclipse

I have this code, which allows me to simulate coin tosses, 0 being heads and 1 being tails or whichever you wish to interpret. 我有这个代码,它允许我模拟硬币投掷,0是头,1是尾巴或任何你想要解释。 When you run the program it randomly generates (in this case) 10 combinations of two coin tosses. 当你运行程序时,它会随机生成(在这种情况下)两个硬币投掷的10种组合。 What I wish to do is modify this program so that the user can ask how many times the coin will be tossed, the coin results will be shown and then he can be prompt to flip again. 我想要做的是修改这个程序,以便用户可以询问投掷硬币的次数,硬币结果将被显示,然后他可以提示再次翻转。

public class Dice {
    public static void main(String[] args)
    {
        for (int i = 0; i <= 10; i++)
        {
            int benito1=(int)(Math.random()*2);
            int benito2=(int)(Math.random()*2);
            System.out.println(benito1 + " " +benito2);
        }
        System.out.println();
    }
}

something like this: 这样的事情:

Scanner sc = new Scanner(System.in);
System.out.prinltn("Please enter a number");
int input = sc.nextInt(); 
while(input-->0)
   {
        int benito1=(int)(Math.random()*2);
              int benito2=(int)(Math.random()*2);
              System.out.println(benito1 + " " +benito2);
            }

Some thing like this : 有点像这样:

public static void main(String[] args) {
        toss();
        System.out.println();
    }

    private static void toss() {
        Scanner get = new Scanner(System.in);
        System.out.println("Enter the limit ...");
        int limit = get.nextInt();
        for (int i = 0; i < limit; i++) {
            int benito1 = (int) (Math.random() * 2);
            int benito2 = (int) (Math.random() * 2);
            System.out.println(benito1 + " " + benito2);
        }
        System.out.println("would you like to continue>");
        String ans = get.next();
        if(ans.equalsIgnoreCase("y") || ans.equalsIgnoreCase("yes")) {
            toss();
        }

    }

You can use Scanner class to get the user inputs like below : 您可以使用Scanner类来获取用户输入,如下所示:

   Scanner scan = new Scanner(System.in);

    int count = scan.nextInt(); 
    for (int i = 0; i < count ; i++)
            {
              int benito1=(int)(Math.random()*2);
              int benito2=(int)(Math.random()*2);
              System.out.println(benito1 + " " +benito2);
            }

You can do some thing like this 你可以做这样的事情

public class Dice {
    public static void main(String[] args)
    {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a value : ");
        int numberOfTimes = scanner.nextInt();
        for (int i = 0; i <= numberOfTimes; i++)
        {
            int benito1=(int)(Math.random()*2);
            int benito2=(int)(Math.random()*2);
            System.out.println(benito1 + " " +benito2);
        }
        System.out.println();
    }
}

Use Scanner to get input from console. 使用Scanner从控制台获取输入。 A simple example is here: 这里有一个简单的例子:

import java.util.Scanner;
public class Dice {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while(true) {

            System.out.println("Enter a value : ");
            int n = scanner.nextInt();
            if(n == 0) {
                break;
            }
            for (int i = 0; i < n; i++) {
                int benito1 = (int) (Math.random() * 2);
                int benito2 = (int) (Math.random() * 2);
                System.out.println(benito1 + " " + benito2);
            }
        }
    }
}
    public class Dice {
    public static void main(String[] args)
    {
    int count = 0;
    System.out.println("Enter The No Of Times : ");
    try
    {
         count = Integer.ParseInt((new BufferedReader(new InputStreamReader(System.in)).readLine());
    for (int i = 0; i <= count; i++)
    {
    int benito1=(int)(Math.random()*2);
        int benito2=(int)(Math.random()*2);
    System.out.println(benito1 + " " +benito2);
    }
    System.out.println();
    }
}

Alternate for Math.random()*2 is, 替代Math.random()* 2是,

 Random rand = new random();
 int benito1 = rand.nextInt(2);
 int benito2 = rand.nextInt(2);

Depends on whether you want a kind of a UI. 取决于您是否想要一种UI。 You could easyily create message boxes which ask for user values and show results. 您可以轻松创建询问用户值和显示结果的消息框。 An example could look like this: 示例可能如下所示:

    boolean repeat = true;
    while (repeat) {
        String strTosses = JOptionPane.showInputDialog(null, "Please enter number of coin tosses", 10);
        int tosses;
        try {
            tosses = Integer.parseInt(strTosses);
        }
        catch (NumberFormatException ex) {
            continue;
        }

        for (int i = 0; i <= tosses; i++) {
            int benito1 = (int)(Math.random() * 2);
            int benito2 = (int)(Math.random() * 2);
            System.out.println(benito1 + " " + benito2);
        }

        int again = JOptionPane.showConfirmDialog(null, "Do you want to try again", "Try again", JOptionPane.YES_NO_OPTION);
        if (again == JOptionPane.NO_OPTION) {
            repeat = false;
        }
    }

Integer.parseInt() may throw a NumberFormatException in case the value entered is not a number. 如果输入的值不是数字,Integer.parseInt()可能会抛出NumberFormatException。 THe program will catch the error and retry. 该程序将捕获错误并重试。

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

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