Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

2 Answers

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);
            }
like image 86
Kamlesh Arya Avatar answered Dec 02 '25 23:12

Kamlesh Arya


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

    }
like image 40
Sandeep Avatar answered Dec 02 '25 21:12

Sandeep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!