簡體   English   中英

如何根據用戶輸入進行硬幣翻轉模擬工作? java eclipse

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

我有這個代碼,它允許我模擬硬幣投擲,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();
    }
}

這樣的事情:

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

有點像這樣:

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

    }

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

你可以做這樣的事情

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

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

替代Math.random()* 2是,

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

取決於您是否想要一種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。 該程序將捕獲錯誤並重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM