for循环和摘要,我们在其中重新分配变量

任务是:编写一个程序,该程序交换给定数组的最大和最小元素。在此操作之前和之后打印阵列。

在编程方面,我仍然是一个菜鸟,在大多数时候,我只是无法真正想象代码在做什么。

public class problem6 {
    public static void main(String[] args) {
        int[] arr = new int[15];
        for (int i = 0; i < arr.length; i++)
/*
I don't think I really understand incrementation in loops,what exactly do they do? As far as I understand 
it is like having an industry line? I suppose 'i' would be the product and '++' would be the thing that 
carries 'i' to the be checked one by one? I really need an ape analogy because I can't really visualize it.
*/
            arr[i] = (int) (Math.random() * 15);

        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + ",");
        System.out.println();

        int minIndex = 0,maxIndex = 0; //why do we assign min and max to 0 and not any other number?
        for (int i = 1; i < arr.length; i++) {
            if (arr[minIndex] > arr[i])
                minIndex = i;
            if (arr[maxIndex] < arr[i])
                maxIndex = i;
/*
I see a lot of codes like:

if (arr[minIndex] > arr[i])
                minIndex = i;
            if (arr[maxIndex] < arr[i])
                maxIndex = i;
what purpose does it serve? For the lack of knowledge and from a really noob programmer's perspective
it seems like we are "reassigning" minIndex and maxIndex to 'i',which is probably not what we're actually doing
but I don't know how to describe it other way.
*/
        }
        System.out.println(minIndex + " " + arr[minIndex]);
        System.out.println(maxIndex + " " + arr[maxIndex]);


        {

            int tmp = arr[minIndex];
            arr[minIndex] = arr[maxIndex];
            arr[maxIndex] = tmp;
/*
Here again,for the lack of understanding and better way of describing,it seems to me that we are reassigning,which makes it appear illogical to me,but I'd assume it's not actually "reassigning".

int tmp = arr[minIndex];
            arr[minIndex] = arr[maxIndex];
            arr[maxIndex] = tmp;
*/
        }

        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + ",");
        System.out.println();
    }
}
dingdingzhengwei 回答:for循环和摘要,我们在其中重新分配变量

这是为您快速解决的问题。

以下是交换给定数组的最大和最小元素的完整示例。

输入:

在数组中输入要输入的元素: 5

输入所有元素: 10 15 16 1个 9

输出:

交换后的元素: 10 15 1个 16 9

public static void main(String arg[]) {
        Scanner scan = null;
        int number;
        try {
            scan = new Scanner(System.in);
            System.out.println("Enter elements you want to input in array: ");
            number = scan.nextInt();
            int array[] = new int[number];
            System.out.println("Enter all the elements:");

            for (int i = 0; i < number; i++) {
                array[i] = scan.nextInt();
            }
            int[] resultArray = swap(array);
            System.out.println("Element After swaps :");
            for (int i : resultArray) {
                System.out.println(i);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            scan.close();
        }
    }

    public static int[] swap(int[] array) {
        int minIndex = 0,maxIndex = 0;
        for (int i = 1; i < array.length; ++i) {
            if (array[i] < array[minIndex])
                minIndex = i;
            if (array[i] > array[maxIndex])
                maxIndex = i;
        }
        int t;
        if (maxIndex != minIndex) {
            t = array[minIndex];
            array[minIndex] = array[maxIndex];
            array[maxIndex] = t;
        }
        return array;
    }

希望此解决方案有效。 如果此解决方案有帮助,请标记为答案。

,

i是什么,它是做什么的,i++是什么意思:

在该循环中使用

i来指示您已经循环了该循环多少次。

每次通过for循环循环后,++都可以执行i + 1

为更好地理解示例中的i用法,请想象数组arr是一本大书,而在[]中写的内容是您要打开的页面。在循环中,您基本上将书翻阅到了这些括号中的每个页码。

为什么minIndexmaxIndex设置为0而不是其他任何数字?

将它们设置为0,因为我们可以假定那是数组中最低的索引。因此,当索引与可能是最低或最高索引的任何其他可能的数字进行比较时,它总是被设置为所述最低或最高索引,而不是总是具有您设置的相同值,因为数组中没有其他值会更高或更低比起那个来说。

我们为什么要使arr[minIndex]的值与arr[maxIndex]相同?

在问题标题中,您说过您想将数组中的最高编号与最低编号交换。因此,既然我们已经找到了阵列中的最高和最低编号,则可以通过以下步骤将两者切换。

如果您还有其他疑问,或者我的描述不清楚,只需在答案下方评论,我会尽力帮助您。另外,不要太看不起自己。在某个时候,每个人都是“菜鸟”:)

本文链接:https://www.f2er.com/3168021.html

大家都在问