Create a Java program that asks the user to enter four pairs

Create a Java program that asks the user to enter four pairs of 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 us 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. See below for a sample of runs. ----jGRASP exec: java Pairs_A1 Enter four numbers all between 0 and 9: 1 0 1 1 There are 1 pair ----jGRASP: operation complete. ----jGRASP exec: java pairs_A1 Enter four numbers all between 0 and 9: 7 9 1 2 There are 0 pair ----jGRASP: operation complete. ----jGRASP exec: java Pairs_A1 Enter four numbers all between 0 and 9: 1 2 3 1 There are 1 pair ----jGRASP: operation complete. ------jGRASP exec: java Pairs_Al Enter four numbers all between 0 and 9: 3 2 3 2 There are 2 pair ----jGRASP: operation complete. Provide a printout of properly formatted source code (your entire Java program). Provide 4 example outputs/test cases: showing all four possibilities - no pairs, 1 pair, 2 pairs and a case with 1 pair when three numbers match.

Solution

PairTest.java

import java.util.Scanner;


public class PairTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter four numbers all between 0 and 9: \");
       int pairCount = 1;
       int totalPair = 0;
       int a[] = new int[4];
       for(int i=0; i<4; i++){
           a[i] = scan.nextInt();
       }
       for(int i=0; i<4; i++){
           for(int j=i+1; j<4; j++){
               if(a[i] == a[j]){
                   pairCount++;
                  
               }
              
           }
           if(pairCount>2){
               totalPair = totalPair+pairCount/2;
               break;
           }
           else{
               totalPair = totalPair+pairCount/2;
               pairCount=1;
           }
       }
       System.out.println(\"There are \"+totalPair+\" pair\");
   }

}

Output:

Enter four numbers all between 0 and 9: 1 0 1 1
There are 1 pair

Enter four numbers all between 0 and 9: 9 9 9 9
There are 2 pair

 Create a Java program that asks the user to enter four pairs of numbers between 0-9, then checks to see how many pairs of numbers there are (in any order). The
 Create a Java program that asks the user to enter four pairs of numbers between 0-9, then checks to see how many pairs of numbers there are (in any order). The

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site