Create a Java program that asks the user to enter four numbers between 0-9, then
ID: 3928584 • Letter: C
Question
Create a Java program that asks the user to enter four numbers between 0-9, then checks to see how many pairs of numbers there are (in any order). There can be 0 pairs, 1 pair, or 2 pairs. Once a number is assigned to a pair, it cannot be used to form a different pair. For example:
If the user enters 0 1 0 1 the output would be 2 pair.
If the user enters 9 9 5 5 the output would be 2 pair.
If the user enters 9 9 9 9 the output would be 2 pair.
If the user enters 9 9 9 5 the output would be 1 pair.
If the user enters 3 2 1 3 the output would be 1 pair.
If the user enters 5 6 7 1 the output would be 0 pair.
If the user enters 1 0 1 1 the output would be 1 pair.
Explanation / Answer
import java.util.Scanner;
public class Pair
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int p2=0,p3=0;
int a,b,c,d;
System.out.println(" enter a b c and d:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
if(a==b)
{
p2++;
if(c==d)p2++;
}
if(b==c)
{
p2++;
if(a==d)
p2+;
}
if(a==c)
{
p2++;
if(b==d)p2++;
}
if(a==b&&b==c)
p3++;
if(b==c&&c==d)p3++;
if(a==c&&c==d)p3++;
System.out.println(p2+" two element pairs ");
System.out.println(p3+" three element pairs");
}
}