Advertisement
NikolayDimitrov

java for loop even/odd position

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