简体   繁体   中英

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. When you run the program it randomly generates (in this case) 10 combinations of two coin tosses. 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 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. 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,

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

Depends on whether you want a kind of a 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. THe program will catch the error and retry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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