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

时间:2014-02-14 05:59:49

标签: java eclipse loops random simulation

我有这个代码,它允许我模拟硬币投掷,0是头,1是尾巴或任何你想要解释的。当你运行程序时,它会随机生成(在这种情况下)两个硬币投掷的10种组合。我想要做的是修改这个程序,以便用户可以询问投掷硬币的次数,硬币结果将被显示,然后他可以提示再次翻转。

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

7 个答案:

答案 0 :(得分:1)

类似的东西:

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

答案 1 :(得分:1)

有些事情是这样的:

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

    }

答案 2 :(得分:0)

您可以使用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);
            }

答案 3 :(得分:0)

你可以做这样的事情

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

答案 4 :(得分:0)

使用Scanner从控制台获取输入。这里有一个简单的例子:

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

答案 5 :(得分:0)

    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,

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

答案 6 :(得分:0)

取决于您是否想要一种UI。您可以轻松创建询问用户值和显示结果的消息框。一个例子可能如下所示:

    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()可能会抛出NumberFormatException。该程序将捕获错误并重试。