如何交换数字的位置

时间:2014-11-15 03:14:17

标签: java arrays swap

我是Java编程的新手,我想知道如何交换最高编号和最低编号的位置。

import java.util.*;

public class HighestandLowestNum 
{
    static Scanner data = new Scanner (System.in);

    public static void main(String[]args)
    {
        int num [] = new int [10];
        int i;

        System.out.println("Enter 10 numbers: ");
        for (i = 0; i < num.length; i++)
        {
            num [i] = data.nextInt();
            Highest(num);
            Lowest(num);
        }
        System.out.println("The highest number is: " + Highest(num));
        System.out.println("The lowest number is: " + Lowest(num));     
    }

    public static int Highest(int[] num)    
    {
        int highest = num[0];
        int i;

        for (i = 1; i < num.length; i++)
        {
            if (num[i] > highest)
            {
                highest = num[i];
            }
        }
        return highest;
    }

    public static int Lowest(int[] num)
    {
        int lowest = num[0];
        int i;

        for (i = 1; i < num.length; i++)
        {
            if (num[i] < lowest)
            {
                lowest = num[i];
            }
        }
        return lowest;
    }
}

这是我的计划。请帮我解决我的问题。

2 个答案:

答案 0 :(得分:0)

最高和最低两个函数返回最高和最低数字的value。为了交换它们,您需要知道数组中这些值的位置

如果您更改这些功能或创建新功能以返回最高和最低值的索引,那么您可以将它们与您之前可能已经看过的内容交换:

伪代码:

temp = first
first = last
last = temp

答案 1 :(得分:0)

要交换数组中的两个数字,您需要知道数字及其位置,如前面的答案中所述。您可以修改这两个函数,使它们提供 Numbers 以及 Position 。一种方法是从函数返回一个数组而不是整数。在该阵列中,可以封装有关及其位置的信息。我会在我提供的代码中向您展示我的意思:

static Scanner data = new Scanner (System.in);

public static void main(String[]args)
{
    int num []= new int [10];
    int i;

    System.out.println("Enter 10 numbers: ");
    for (i = 0; i <num.length; i++)
    {
        num [i] = data.nextInt();
        Highest(num);
        Lowest(num);
    }

    System.out.print("The original array is");
    for (i = 0; i <num.length; i++)
    {
        System.out.print(" "+num[i]);

    }
    System.out.println("");

    int highest = Highest(num)[0];
    int lowest = Lowest(num)[0];
    int highestPosition = Highest(num)[1];
    int lowestPosition = Lowest(num)[1];


    System.out.println("The highest number is: " +highest+" at position "+highestPosition);
    System.out.println("The lowest number is: " +lowest+" at position "+lowestPosition);  

    //Swap//

    num[highestPosition] = lowest;
    num[lowestPosition] = highest;

    System.out.print("The new array is");
    for (i = 0; i <num.length; i++)
    {
        System.out.print(" "+num[i]);

    }



}

public static int[] Highest(int[] num)    
{
    int highest = num[0];
    int i;
    int index = 0;
    int[] result = {highest,index};

    for (i = 1; i <num.length;i++)
    {
        if (num[i] > highest)
        {
            highest = num[i];
            index = i;
            result[0] = highest;
            result[1] = index;
        }
    }
    return result;


}

public static int[] Lowest(int[] num)    
{
    int lowest = num[0];
    int i;
    int index = 0;
    int[] result = {lowest,index};

    for (i = 1; i <num.length;i++)
    {
        if (num[i] < lowest)
        {
            lowest = num[i];
            index = i;
            result[0] = lowest;
            result[1] = index;
        }
    }
    return result;


}

尝试阅读最高和最低功能中的修改,然后阅读主要代码。希望这可以帮助。