Advertisement
klasscho

Untitled

Oct 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner num = new Scanner(System.in);
  6. System.out.println("TThis program displays the numbers of the array elements whose total is the maximum");
  7. int[] a, b, aNew;
  8. int n, maxsum, maxind;
  9. boolean isInCorrect;
  10. final int MinNumb = 0, MaxNumb = 1073741823;
  11. do {
  12. isInCorrect = false;
  13. System.out.println("Enter a length of array [1..20]: ");
  14. n = num.nextInt();
  15. if ((n < 1) || (n > 20)) {
  16. System.out.println("Enter a value between [1..20]");
  17. isInCorrect = true;
  18. }
  19. }
  20. while (isInCorrect);
  21. a = new int[n];
  22. b = new int[n];
  23. aNew = new int[n];
  24. for (int i = 0; i < n; i++) {
  25. do {
  26. isInCorrect = false;
  27. System.out.format("Enter a sequence item a[" + i + "]. He is must be more %d, but less than %d\n", MinNumb, MaxNumb);
  28. a[i] = num.nextInt();
  29. if ((a[i] < MinNumb) || (a[i] > MaxNumb)) {
  30. System.out.format("Enter a value between [%d..%d]\n", MinNumb, MaxNumb);
  31. isInCorrect = true;
  32. }
  33. }
  34. while (isInCorrect);
  35. }
  36. for (int i = 0; i < n; i++) {
  37. do {
  38. isInCorrect = false;
  39. System.out.format("Enter a sequence item b[" + i + "]. He is must be more %d, but less than %d\n", MinNumb, MaxNumb);
  40. b[i] = num.nextInt();
  41. if ((b[i] < MinNumb) || (b[i] > MaxNumb)) {
  42. System.out.format("Enter a value between [%d..%d]\n", MinNumb, MaxNumb);
  43. isInCorrect = true;
  44. }
  45. }
  46. while (isInCorrect);
  47. }
  48. 3
  49. maxsum = 0;
  50. maxind = 0;
  51. for (int i = 0; i < n; i++) {
  52. if (a[i] + b[i] > maxsum) {
  53. maxsum = a[i] + b[i];
  54. maxind = i;
  55. }
  56. }
  57. System.out.println(maxind);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement