Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main{
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- Pair biggestPair = null;
- boolean done = false;
- int count = 0, biggestSum = Integer.MIN_VALUE;
- while (!done) {
- System.out.println("Please enter two numbers (on two different lines)");
- Pair current = new Pair(in.nextInt(), in.nextInt());
- if (current.getLeft()%2 == 0 && current.getRight()%2 != 0) {
- count++;
- int currentSum = current.getLeft() + current.getRight();
- if (currentSum > biggestSum) {
- biggestSum = currentSum;
- biggestPair = current;
- }
- } else if (current.getLeft()%2 != 0 && current.getRight()%2 == 0) {
- done = true;
- }
- }
- if (biggestPair != null) {
- System.out.println("The pair with the greatest sum is: " + biggestPair + ". Their sum is: " + biggestSum + ".");
- System.out.println("The number of valid pairs: " + count + ".");
- } else {
- System.out.println("There is no valid pair");
- }
- }
- }
- class Pair {
- private int Left, Right;
- public Pair(int Left, int Right) {
- this.Left = Left;
- this.Right = Right;
- }
- public int getLeft() {
- return Left;
- }
- public int getRight() {
- return Right;
- }
- public String toString() {
- return String.format("Pair [Left=%d, Right=%d]", Left, Right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement