如何将最大数字与数组中的最后一个数字交换?

时间:2016-11-23 20:03:02

标签: java arrays algorithm swap jcreator

我正在尝试创建一个程序,要求用户输入一个数字,即在数组中生成1-100之间的随机数。然后我希望最大的数字与最后一个数字交换,最小的数字与第一个数字交换。到目前为止,这是我的代码:

import java.util.Scanner;
import java.util.Random;

public class smallbig
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        int num = scan.nextInt();
        int[] myArray = new int[num];

        for (int i = 0; i < myArray.length; ++i) {
            int randomInt = randomGenerator.nextInt(100);
            myArray[i] = randomInt;
        }
        int smallest = myArray[0];
        int largest = myArray[0];

        for (int i = 1; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
            }
        }

        for (int j = 1; j < myArray.length; j++) {
            int first = myArray[0];
            myArray[0] = smallest;
            smallest = first;

            int temp = largest;
            int last = myArray.length;
            myArray[last - 1] = largest;
            temp = myArray[last - 1];

            System.out.print(myArray[j] + " ");
        }
    }
}

我似乎无法让数字正确交换。我创建了一个循环,它确定生成的数字中最小和最大的数字,并存储这些数字。然后我创建一个循环,执行必要的交换,但我似乎无法让它正常工作。它适用于将最大数字与最后一个数字交换,但大多数时间(并非总是)输出的最后一个数字也存在于数组中的其他位置。这就是我的意思:

input: 10
output: 62 48 34 0 91 14 64 60 91

我知道我可以使用一种交换方法,但我想通过手动交换数字来实现。感谢任何帮助。谢谢。

7 个答案:

答案 0 :(得分:3)

你有一个简单的错误,当你执行交换

时,你的循环应该从'0'开始
for (int j = 0; j < myArray.length; j++) {
        int first = myArray[0];
        myArray[0] = smallest;
        smallest = first;

        int temp = largest;
        int last = myArray.length;
        myArray[last - 1] = largest;
        temp = myArray[last - 1];

        System.out.print(myArray[j] + " ");

答案 1 :(得分:1)

而不是循环

//find and stores poition of small
int smallPos = myArray.indexOf(small);

//stores tge values at 0
int tempSmall = myArray[0];

//swaps the values
myArray[0] = small;
myArray[smallPos] = smallTemp;

只需使用最大值重复此操作并使用for循环打印它。告诉我它是否有效。

答案 2 :(得分:1)

我刚试过这个。希望这会有所帮助。

public class App {
static int[] a = new int[100];
public static void main(String[] args) {
    int i;
    for(i = 0; i<a.length;i++)
        a[i] = (int)(java.lang.Math.random() * 100);

    int smallest = 0, largest = 0;

    for(i =1; i<a.length; i++){
        if(a[i] < a[smallest])
            smallest = i;
        if(a[i] > a[largest])
            largest = i;
    }

    swap(0,smallest);
    swap(a.length-1,largest);

    for(i =0; i<a.length;i++)
        System.out.print(a[i] + " ");
}

public static void swap(int i, int j){
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}
}

答案 3 :(得分:0)

你的上一个循环没有做任何循环的事情。它只是一遍又一遍地做同样的事情。它的作用是不正确的。它将最小的元素交换到第一个元素中,但它并没有将第一个元素移动到任何地方:它已经消失了。

您需要跟踪最大和最小元素的位置。您的交换逻辑不考虑这一点。

答案 4 :(得分:0)

编写此代码

import java.util.Scanner;
import java.util.Random;

public class smallbig
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        int num = scan.nextInt();
        int[] myArray = new int[num];

        for (int i = 0; i < myArray.length; ++i) {
            int randomInt = randomGenerator.nextInt(100);
            myArray[i] = randomInt;
        }
        int smallest = myArray[0];
        int largest = myArray[0];
        int pos1,pos2;

        for (int i = 1; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
                pos1=i;
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
               pos2=i;
              }
        }



myArray[pos1]=myArray[myArray.length-1];
myArray[myArray.length-1]=largest;

myArray[pos2]=myArray[0];
myArray[0]=smallest;

        for (int j = 1; j < myArray.length; j++) {


            System.out.print(myArray[j] + " ");
        }
    }
}

答案 5 :(得分:0)

我建议不要使用扫描仪或随机值进行测试,因为结果无法再现(至少不是很简单)。小心什么是数组索引以及特定索引的值是多少。这可能会很快导致混淆。 ^^

import java.util.Scanner;
import java.util.Random;



public class smallbig
{

    private static int[] myArray;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        //int num = scan.nextInt(); //skip this for testing ^^-d
        int num = 10;
        myArray = new int[num];

        for (int i = myArray.length-1; i > 0; i--) {
            //int randomInt = randomGenerator.nextInt(100);
            int randomInt = i; //use test condition that can be reproduced!
            myArray[i] = myArray.length-i;
        }
        int smallest = 0;
        int largest = 0;

        int smallestIndex = 0;
        int largestIndex = 0;

        for (int i = 0; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
                largestIndex = i;
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
                smallestIndex = i;
            }
        }

        switchIndexOfmyArray(0, smallestIndex);

        switchIndexOfmyArray(myArray.length-1, largestIndex);

        for (int j = 0; j < myArray.length; j++) {
            System.out.print(myArray[j] + " ");
        }

    }
    public static void switchIndexOfmyArray(int index, int switchWithIndex){
        int temp = myArray[index];
        myArray[index] = myArray[switchWithIndex];
        myArray[switchWithIndex] = temp;
    }
}

屈服

0 1 8 7 6 5 4 3 2 9 

我承认,由于我感到饥饿和疲惫,我在这个问题上的速度很慢xD

快乐的编码! ^^

答案 6 :(得分:0)

通过找到最小和最大的你做得很好。问题在于交换。我重构了swap方法。可能是有效的。

import java.util.Scanner;
import java.util.Random;

public class smallbig
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        int num = scan.nextInt();
        int[] myArray = new int[num];

        for (int i = 0; i < myArray.length; ++i) {
            int randomInt = randomGenerator.nextInt(100);
            myArray[i] = randomInt;
        }


        for(int k=0; k < myArray.length; k++)
        {
            System.out.print(myArray[k] + " ");
        }

        System.out.println();

        int smallest = myArray[0];
        int largest = myArray[0];

        int sIndx = 0;
        int lIndx = 0;

        for (int i = 1; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
                lIndx = i;
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
                sIndx = i;
            }
        }

        System.out.println();
        System.out.println("Smallest = "+smallest);
        System.out.println("largest = "+largest);
        System.out.println("Smallest Index = "+sIndx);
        System.out.println("largest Index = "+lIndx);

        swapSmallAndLargest(num, myArray, sIndx, lIndx);

        for(int k=0; k < myArray.length; k++)
        {
            System.out.print(myArray[k] + " ");
        }


    }

    private static void swapSmallAndLargest(int num, int[] myArray, int sIndx, int lIndx) {
        int temp = 0;

        temp = myArray[sIndx];
        myArray[sIndx] = myArray[0];
        myArray[0] = temp;

        temp = myArray[lIndx];
        myArray[lIndx] = myArray[num-1];
        myArray[num-1] = temp;
    }
}