Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main{
  4.   public static void main(String args[]) {
  5.     Scanner in = new Scanner(System.in);
  6.     Pair biggestPair = null;
  7.     boolean done = false;
  8.     int count = 0, biggestSum = Integer.MIN_VALUE;
  9.    
  10.     while (!done) {
  11.       System.out.println("Please enter two numbers (on two different lines)");
  12.       Pair current = new Pair(in.nextInt(), in.nextInt());
  13.      
  14.       if (current.getLeft()%2 == 0 && current.getRight()%2 != 0) {
  15.         count++;
  16.         int currentSum = current.getLeft() + current.getRight();
  17.        
  18.         if (currentSum > biggestSum) {
  19.           biggestSum = currentSum;
  20.           biggestPair = current;
  21.         }
  22.       } else if (current.getLeft()%2 != 0 && current.getRight()%2 == 0) {
  23.         done = true;
  24.       }
  25.     }
  26.    
  27.     if (biggestPair != null) {
  28.       System.out.println("The pair with the greatest sum is: " + biggestPair + ". Their sum is: " + biggestSum + ".");
  29.       System.out.println("The number of valid pairs: " + count + ".");
  30.     } else {
  31.       System.out.println("There is no valid pair");
  32.     }
  33.   }
  34. }
  35.  
  36. class Pair {
  37.   private int Left, Right;
  38.  
  39.   public Pair(int Left, int Right) {
  40.     this.Left = Left;
  41.     this.Right = Right;
  42.   }
  43.  
  44.   public int getLeft() {
  45.     return Left;
  46.   }
  47.  
  48.   public int getRight() {
  49.     return Right;
  50.   }
  51.  
  52.   public String toString() {
  53.     return String.format("Pair [Left=%d, Right=%d]", Left, Right);
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement