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

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

Swapping arrays between min and max

问题

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

我的代码:

import java.util.Scanner;

public class Main {
    public static void main(String []args) {
        Scanner obj = new Scanner(System.in);
        int[] arr = new int[10];
        System.out.println("输入10个随机数:");
        
        for(int i = 0; i < arr.length; i++) {
            int num = obj.nextInt();
            arr[i] = num;
        }

        int minIndex = findMinIndex(arr, arr.length);
        int maxIndex = findMaxIndex(arr, arr.length);
        
        // 交换最大和最小数的位置
        int temp = arr[minIndex];
        arr[minIndex] = arr[maxIndex];
        arr[maxIndex] = temp;

        System.out.print("交换后的数组: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }

    static int findMaxIndex(int[] numbers, int length) {
        int maxIndex = 0;
        
        for (int i = 1; i < length; i++) {
            if (numbers[i] > numbers[maxIndex]) {
                maxIndex = i;
            }
        }

        return maxIndex;
    }

    static int findMinIndex(int[] numbers, int length) {
        int minIndex = 0;
        
        for (int i = 1; i < length; i++) {
            if (numbers[i] < numbers[minIndex]) {
                minIndex = i;
            }
        }

        return minIndex;
    }
}

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

输入: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:

import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner obj = new Scanner(System.in);
int[] arr  =new int[10];
System.out.println(&quot;enter 10 random numbers: &quot;);
for(int i = 0 ; i &lt; arr.length; i++){
int num = obj.nextInt();
arr[i] = num;
}
int min = findMin(arr, arr.length);
int max =findMax(arr, arr.length);
System.out.println(&quot;max number : &quot; + max + &quot; | min number : &quot; + min);
}
static int findMax(int[] numbers, int lenght) {
int max = numbers[0];
for (int i = 1; i &lt; lenght; i++) {
if (numbers[i] &gt; max) {
max = numbers[i];
}
}
System.out.println();
return max;
}
static int findMin(int[] numbers, int lenght) {
int min = numbers[0];
for (int i = 1; i &lt; lenght; i++) {
if (numbers[i] &lt; min) {
min = numbers[i];
}
}
return min;
}
}

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

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int[] nums = new int[10];

    System.out.println("请输入10个整数:");
    for (int i = 0; i < nums.length; i++) {
        nums[i] = scan.nextInt();
    }
    int max = nums[0];
    int min = nums[0];
    for (int i = 0; i < nums.length; i++) {
        if (min > nums[i]) {
            min = nums[i];
        } else if (max < nums[i]) {
            max = nums[i];
        }
    }
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == min) {
            System.out.print(max + ", ");
        } else if (nums[i] == max) {
            System.out.print(min + ", ");
        } else {
            System.out.print(nums[i] + ", ");
        }
    }
    System.out.println("\n最大值为:" + max);
    System.out.println("最小值为:" + min);
}
英文:
    public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] nums = new int [10];
System.out.println(&quot;Please enter 10 ints: &quot;);      
for(int i = 0;i&lt;nums.length;i++){
nums[i] = scan.nextInt();
}
int max = nums[0];
int min = nums[0];
for(int i = 0;i&lt;nums.length;i++) {
if(min&gt;nums[i]) {
min = nums[i];
}else if(max&lt;i) {
max = nums[i];
}
}
for(int i = 0;i&lt;nums.length;i++) {
if(nums[i]==min) {
System.out.print(max+&quot;, &quot;);
}else if(nums[i]==max) {
System.out.print(min+&quot;, &quot;);
}else {
System.out.print(nums[i]+&quot;, &quot;);
}
}
System.out.println(&quot;\nmax is: &quot;+max);
System.out.println(&quot;min is: &quot;+min); 
}

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

答案2

得分: 0

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

min = integer[0];
max = integer[0];

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

integer[minIndex] = max;
integer[maxIndex] = min;

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

英文:

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

min = integer[0];
max = integer[0];

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

   integer[minIndex] = max;
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:

确定