Advertisement
finderabc

OddAndEvenPosition( For-Loop - Exercise )

Dec 14th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. public class     OddAndEvenPosition {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         DecimalFormat df = new DecimalFormat("#.###");
  8.  
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.  
  11.         double evenSum = 0.0;
  12.         double maxEven = -1000000000.0;
  13.         double minEven = 1000000000.0;
  14.  
  15.         double oddSum = 0.0;
  16.         double maxOdd = -1000000000.0;
  17.         double minOdd = 1000000000.0;
  18.  
  19.         for (int i = 1; i <= n; i++) {
  20.             double num = Double.parseDouble(scanner.nextLine());
  21.             if (i % 2 == 0) {
  22.                 evenSum = evenSum + num;
  23.                 if (num < minEven) {
  24.                     minEven = num;
  25.                 }
  26.                 if (num > maxEven) {
  27.                     maxEven = num;
  28.                 }
  29.             } else {
  30.                 oddSum = oddSum + num;
  31.                 if (num < minOdd) {
  32.                     minOdd = num;
  33.                 }
  34.                 if (num > maxOdd) {
  35.                     maxOdd = num;
  36.                 }
  37.             }
  38.  
  39.         }
  40.         System.out.printf("OddSum=%s%n", df.format(oddSum));
  41.         if (minOdd == 1000000000.0) {
  42.             System.out.println("OddMin=No");
  43.         } else {
  44.             System.out.printf("OddMin=%s%n", df.format(minOdd));
  45.         }
  46.         if (maxOdd == -1000000000.0) {
  47.             System.out.println("OddMax=No");
  48.         } else {
  49.             System.out.printf("OddMax=%s%n", df.format(maxOdd));
  50.         }
  51.         System.out.printf("EvenSum=%s%n", df.format(evenSum));
  52.         if (minEven == 1000000000.0) {
  53.             System.out.println("EvenMin=No");
  54.         } else {
  55.             System.out.printf("EvenMin=%s%n", df.format(minEven));
  56.         }
  57.         if (maxEven == -1000000000.0) {
  58.             System.out.println("EvenMax=No");
  59.         } else {
  60.             System.out.printf("EvenMax=%s%n", df.format(maxEven));
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement