Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于我们重新分配变量的循环和代码段_Java_Loops - Fatal编程技术网

Java 对于我们重新分配变量的循环和代码段

Java 对于我们重新分配变量的循环和代码段,java,loops,Java,Loops,任务是:编写一个程序,交换给定数组的最大和最小元素。在此操作之前和之后打印阵列 我仍然是一个编程高手,大多数时候我真的无法想象代码在做什么 公共类问题6{ 公共静态void main(字符串[]args){ int[]arr=新int[15]; 对于(int i=0;i

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

我仍然是一个编程高手,大多数时候我真的无法想象代码在做什么

公共类问题6{
公共静态void main(字符串[]args){
int[]arr=新int[15];
对于(int i=0;iarr[i])
minIndex=i;
if(arr[maxIndex]arr[i])
minIndex=i;
if(arr[maxIndex]
什么是
i
i++
是什么意思:

i
在该循环中用于指示您在所述循环中循环了多少次

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

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

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

它们被设置为
0
,因为我们可以假设这是数组中可能的最低索引。因此,当索引与可能是最低或最高索引的任何其他可能数字进行比较时,它总是设置为所述最低或最高索引,而不是始终具有与您设置的相同的值,因为数组中没有其他值高于或低于该值

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

在问题的标题中,您说过要将数组中的最高数字与最低数字交换。现在我们已经找到了数组中的最高和最低数字,这些步骤将两者切换


如果您有任何其他问题,或者我的任何描述不清楚,请在答案下方进行评论,我将尽全力帮助您。还有,不要太瞧不起自己。每个人在某个时候都是一个“noob:)

这里有一个快速解决方法

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

输入:

输入要在数组中输入的元素: 五,

输入所有元素: 10 15 16 1. 九,

输出:

互换后的要素: 10 15 1. 16 九,

publicstaticvoidmain(字符串arg[]){
扫描仪扫描=空;
整数;
试一试{
扫描=新扫描仪(System.in);
System.out.println(“输入要在数组中输入的元素:”);
number=scan.nextInt();
int数组[]=新的int[number];
System.out.println(“输入所有元素:”);
for(int i=0;i数组[maxIndex])
maxIndex=i;
}
int t;
如果(最大索引!=最小索引){
t=数组[minIndex];
数组[minIndex]=数组[maxIndex];
数组[maxIndex]=t;
}
返回数组;
}
希望这个解决方案有效。
如果此解决方案有用,请标记为答案。

在尝试使用Java之前,您需要先学习Java;)关于最后一部分,我们让arr[minIndex]断言与arr[maxIndex]相同的值。我们声明int tmp=arr[minIndex]是这样的,以便以后需要arr[max]时
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;
    }