How can I do this coin transfer job based on user input? java eclipse

I have this code that allows me to simulate the tones of coins, 0 - heads and 1 - tails, or depending on what you want to interpret. When you start the program, it randomly generates (in this case) 10 combinations of two coin tones. What I want to do is change this program so that the user can ask how many times the coin will be thrown, the results of the coin will be shown, and then he can request rewind.

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();
    }
}
+3
source share
7 answers

something like that:

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
source

Something like that:

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

    }
+1
source

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

Math.random() * 2 is,

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

Depends on whether you want some kind of user interface. You can easily create message boxes that request custom values ​​and display results. An example might 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 if the value entered is not a number. The program will catch the error and try again.

0
source

All Articles