在最小值和最大值之间交换数组

huangapple go评论100阅读模式
英文:

Swapping arrays between min and max

问题

我被分配任务制作一个程序,该程序从10个随机数中找到最大和最小的数,并交换它们的位置。

我的代码:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String []args) {
  4. Scanner obj = new Scanner(System.in);
  5. int[] arr = new int[10];
  6. System.out.println("输入10个随机数:");
  7. for(int i = 0; i < arr.length; i++) {
  8. int num = obj.nextInt();
  9. arr[i] = num;
  10. }
  11. int minIndex = findMinIndex(arr, arr.length);
  12. int maxIndex = findMaxIndex(arr, arr.length);
  13. // 交换最大和最小数的位置
  14. int temp = arr[minIndex];
  15. arr[minIndex] = arr[maxIndex];
  16. arr[maxIndex] = temp;
  17. System.out.print("交换后的数组: ");
  18. for (int num : arr) {
  19. System.out.print(num + " ");
  20. }
  21. }
  22. static int findMaxIndex(int[] numbers, int length) {
  23. int maxIndex = 0;
  24. for (int i = 1; i < length; i++) {
  25. if (numbers[i] > numbers[maxIndex]) {
  26. maxIndex = i;
  27. }
  28. }
  29. return maxIndex;
  30. }
  31. static int findMinIndex(int[] numbers, int length) {
  32. int minIndex = 0;
  33. for (int i = 1; i < length; i++) {
  34. if (numbers[i] < numbers[minIndex]) {
  35. minIndex = i;
  36. }
  37. }
  38. return minIndex;
  39. }
  40. }

我的问题是在输出行上没有换行,例如:

输入:1 2 3 4 5 6 78 9 5 5

输出:78 2 3 4 5 6 1 9 5 5

英文:

I am tasked to make a program that finds the max and min numbers from 10 random numbers and swap their places.

My code:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String []args) {
  4. Scanner obj = new Scanner(System.in);
  5. int[] arr =new int[10];
  6. System.out.println(&quot;enter 10 random numbers: &quot;);
  7. for(int i = 0 ; i &lt; arr.length; i++){
  8. int num = obj.nextInt();
  9. arr[i] = num;
  10. }
  11. int min = findMin(arr, arr.length);
  12. int max =findMax(arr, arr.length);
  13. System.out.println(&quot;max number : &quot; + max + &quot; | min number : &quot; + min);
  14. }
  15. static int findMax(int[] numbers, int lenght) {
  16. int max = numbers[0];
  17. for (int i = 1; i &lt; lenght; i++) {
  18. if (numbers[i] &gt; max) {
  19. max = numbers[i];
  20. }
  21. }
  22. System.out.println();
  23. return max;
  24. }
  25. static int findMin(int[] numbers, int lenght) {
  26. int min = numbers[0];
  27. for (int i = 1; i &lt; lenght; i++) {
  28. if (numbers[i] &lt; min) {
  29. min = numbers[i];
  30. }
  31. }
  32. return min;
  33. }
  34. }

My problem is i dont get print in line
example
imput 1 2 3 4 5 6 78 9 5 5

output 78 2 3 4 5 6 1 9 5 5

答案1

得分: 0

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. int[] nums = new int[10];
  4. System.out.println("请输入10个整数:");
  5. for (int i = 0; i < nums.length; i++) {
  6. nums[i] = scan.nextInt();
  7. }
  8. int max = nums[0];
  9. int min = nums[0];
  10. for (int i = 0; i < nums.length; i++) {
  11. if (min > nums[i]) {
  12. min = nums[i];
  13. } else if (max < nums[i]) {
  14. max = nums[i];
  15. }
  16. }
  17. for (int i = 0; i < nums.length; i++) {
  18. if (nums[i] == min) {
  19. System.out.print(max + ", ");
  20. } else if (nums[i] == max) {
  21. System.out.print(min + ", ");
  22. } else {
  23. System.out.print(nums[i] + ", ");
  24. }
  25. }
  26. System.out.println("\n最大值为:" + max);
  27. System.out.println("最小值为:" + min);
  28. }
英文:
  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. int [] nums = new int [10];
  4. System.out.println(&quot;Please enter 10 ints: &quot;);
  5. for(int i = 0;i&lt;nums.length;i++){
  6. nums[i] = scan.nextInt();
  7. }
  8. int max = nums[0];
  9. int min = nums[0];
  10. for(int i = 0;i&lt;nums.length;i++) {
  11. if(min&gt;nums[i]) {
  12. min = nums[i];
  13. }else if(max&lt;i) {
  14. max = nums[i];
  15. }
  16. }
  17. for(int i = 0;i&lt;nums.length;i++) {
  18. if(nums[i]==min) {
  19. System.out.print(max+&quot;, &quot;);
  20. }else if(nums[i]==max) {
  21. System.out.print(min+&quot;, &quot;);
  22. }else {
  23. System.out.print(nums[i]+&quot;, &quot;);
  24. }
  25. }
  26. System.out.println(&quot;\nmax is: &quot;+max);
  27. System.out.println(&quot;min is: &quot;+min);
  28. }

this should produce the max and min of a given random array of size 10 with out fault.

答案2

得分: 0

好的,如果您将第12行和第13行更改为(以正确查找最小值和最大值)

  1. min = integer[0];
  2. max = integer[0];

并在末尾添加一些代码块(以进行交换):

  1. integer[minIndex] = max;
  2. integer[maxIndex] = min;

那么就可以了。然后您只需打印数组。

英文:

Alright so if you change the 12th and 13th lines to (to find min and max correctly)

  1. min = integer[0];
  2. max = integer[0];

And add some piece of code at the end like (to swap):

  1. integer[minIndex] = max;
  2. integer[maxIndex] = min;

It would be alright. Then you can just print the array.

huangapple
  • 本文由 发表于 2020年10月8日 05:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64252527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定