Java program that reads four integers and prints "two pairs" if the input consists of two matching pairs and "not two pairs" otherwise
Posted by Samath
Last Updated: February 12, 2021

Write a program that reads four integers and prints “two pairs” if the input consists of two matching pairs (in some order) and “not two pairs” otherwise. For example, 
1 2 2 1 two pairs 
1 2 2 3 not two pairs 
2 2 2 2 two pairs

Code:

import java.util.Scanner;

public class Main
{        
    public static void main(String[] args) {
        double num1;
        double num2;
        double num3;
        double num4;
	    
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter first number: ");
        num1 = input.nextDouble();
        
        System.out.println("Enter second number: ");
        num2 = input.nextDouble();
        
        System.out.println("Enter third number: ");
        num3 = input.nextDouble();
        
        System.out.println("Enter fourth number: ");
        num4 = input.nextDouble();
        
    if (num1 == num2 || num1 == num3 || num1 == num4 || num2 == num3 || num2 == num4 || num3 == num4) {
      System.out.println("Two pairs");
    } else {
      System.out.println("Not two pairs");
    }
    input.close();
  }
}