Advertisement
veronikaaa86

03. Odd / Even Position

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