yovkovbpfps

For Loop Exercise Odd / Even Position

Apr 15th, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class oddEven {
  4. public static void main(String[] args) {
  5. Scanner console = new Scanner (System.in);
  6.  
  7. int n = Integer.parseInt(console.nextLine());
  8. double oddSum = 0.0;
  9. double oddMin = 1000000000.0;
  10. double oddMax = -1000000000.0;
  11. double evenSum = 0.0;
  12. double evenMin = 1000000000.0;
  13. double evenMax = -1000000000.0;
  14.  
  15. for (int i = 1; i <= n; i++){
  16. Double current = Double.parseDouble(console.nextLine());
  17.  
  18. if (i%2==0){
  19. evenSum+=current;
  20. if (current>evenMax){
  21. evenMax=current;
  22. }if (current<evenMin){
  23. evenMin=current;
  24. }
  25.  
  26. }else {
  27. oddSum+=current;
  28. if (current>oddMax){
  29. oddMax= current;
  30. }
  31. if (current < oddMin) {
  32. oddMin= current;
  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.printf("EvenMin=No,%n");
  50. } else {
  51. System.out.printf("EvenMin=%.2f,%n", evenMin);
  52. }
  53. if (evenMax ==-1000000000.0 ){
  54. System.out.printf("EvenMax=No%n");
  55. } else {
  56. System.out.printf("EvenMax=%.2f%n", evenMax);
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment