1

我应该做的是编写一个程序,让用户输入软件名称和库存数量。我需要将它们存储在一个数组中,然后使用选择排序按照从最少到最多的顺序对其进行排序。

我的问题是,我不想把软件的名字和数字分开!此外,当我编译时它没有显示排序的名称!我读过关于选择排序的TONS,它们基本上都是这样的。它有什么问题?我做错了选择排序吗?

这不是我的全部代码,但我认为我没有遗漏任何重要的内容:

// Global variables
static String[] SoftwareArray;
static int[] QuantityArray;

public static void inputInfo() throws IOException
{
    BufferedReader userInput = new BufferedReader  (new InputStreamReader(System.in));  
    System.out.print("How many softwares would you like to input? ");
    String software = userInput.readLine();
    int softwareNum = Integer.parseInt(software);  
    int[] softArray = new int[softwareNum];      

    String [] name = new String [softwareNum];
    int [] quantity = new int[softwareNum];      

    // Initialize global variables
    SoftwareArray = new String[softwareNum];
    QuantityArray = new int[softwareNum];

    //loop through number of softwares  
    for (int i = 0; i < softwareNum; i++)
    {
        System.out.println("Input name of software: ");
        String softwareName = userInput.readLine();

        name[i] = softwareName;

        System.out.println("Input quantity of software: ");
        String quantityString = userInput.readLine();
        int softwareQuantity = Integer.parseInt(quantityString);  

        quantity[i] = softwareQuantity;

        // Copy the software name and quantity to the global variables
        QuantityArray[i] = quantity[i];
        SoftwareArray[i] = name[i];

        System.out.println("There are " + quantity[i] + " of the " + name[i] + " software.");
    }
}

//method to sort and display info
public static void displayInfo(int[] arr, String[] name)
{      
    //sort by quantity
    for(int i=0; i<arr.length; i++)
    {
        for(int j=i+1; j<arr.length; j++)
        {
            if(arr[i] > arr[j] )
            {
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;

                String tempString = name[j];
                name[j] = name[i];
                name[i] = tempString;
            }
        }
        //output
        for(i=0; i < arr.length; i++)
        {
            System.out.println(arr[i] + "  " + name[i]);
        }
    }
}

//main
public static void main(String[] args) throws IOException {
   //input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    inputInfo();

    displayInfo(QuantityArray, SoftwareArray);
}

输出:

How many softwares would you like to input? 2
Input name of software: 
Microsoft
Input quantity of software: 
1000
There are 1000 of the Microsoft software.
Input name of software: 
Linux
Input quantity of software: 
2983
There are 2983 of the Linux software.

然后什么都没有。它根本不显示排序列表。

4

3 回答 3

0

你有一个 for 循环i在你的 for 循环中迭代i

for(int i=0; i<arr.length; i++)
{
    for(int j=i+1; j<arr.length; j++)
    {
        if(arr[i] > arr[j] )
        {
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;

            String tempString = name[j];
            name[j] = name[i];
            name[i] = tempString;
        }
    }
    //output
    for(i=0; i < arr.length; i++)
    {
        System.out.println(arr[i] + "  " + name[i]);
    }
}

我认为您可能打算将第二个循环放在第一个循环之外

我想你可能打算把那个循环

for(int i=0; i<arr.length; i++)
{
    for(int j=i+1; j<arr.length; j++)
    {
        ...
    }
}
//output
for(i=0; i < arr.length; i++)
{
    System.out.println(arr[i] + "  " + name[i]);
}

另一方面,JB Nized 是对的。如果你想让你的名字和数量更耦合,你应该做一个类

public class Software
{
    public int quantity;
    public int string;
}
于 2013-08-27T18:00:34.330 回答
0

你应该改变这一行:

for(int j=i+1; j<arr.length; j++)

至:

for(int j=i; j<arr.length; j++)

完整代码:

public class SelectionSort {

    public static void displayInfo(int[] arr, String[] name)
    {
        //sort by quantity
        for(int i=0; i<arr.length; i++)
        {
            for(int j=i; j<arr.length; j++)
            {
                if(arr[i] > arr[j] )
                {
                    int temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;

                    String tempString = name[j];
                    name[j] = name[i];
                    name[i] = tempString;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] QuantityArray = {3,2,4,1,5};
        String[] SoftwareArray = {"3","2","4","1","5"};
        displayInfo(QuantityArray, SoftwareArray);
        for(int i=0; i<QuantityArray.length; i++){
            System.out.print(QuantityArray[i]+" ");
        }
        System.out.println();
        for(int i=0; i<QuantityArray.length; i++){
            System.out.print(SoftwareArray[i]+" ");
        }
    }
}

输出:

1 2 3 4 5 
1 2 3 4 5 
于 2013-08-27T18:03:31.210 回答
-1

我不确定你为什么特别需要选择排序,因为它保证了 O(n^2) 的复杂性。此外,我觉得外循环应该运行到一定长度 - 1。检查here以使其正确。

此外,目前您的代码提供以下输出:

How many softwares would you like to input? 3

Input name of software: windows
Input quantity of software: 3
There are 3 of the windows software.

Input name of software: google
Input quantity of software: 2
There are 2 of the google software.

Input name of software: office
Input quantity of software: 4
There are 4 of the office software.

2  google
3  windows
4  office

您是否期望有所不同,如果是,请在问题中添加。此外,这不是选择排序。这对我来说似乎更像是一种泡沫。

编辑粘贴您在我的机器上运行的代码以产生上述输出。导入 java.io.BufferedReader;导入 java.io.IOException;导入 java.io.InputStreamReader;

public class Test {

    // Global variables
    static String[] SoftwareArray;
    static int[] QuantityArray;

    public static void inputInfo() throws IOException {
    BufferedReader userInput = new BufferedReader(new InputStreamReader(
            System.in));
    System.out.print("How many softwares would you like to input? ");
    String software = userInput.readLine();
    int softwareNum = Integer.parseInt(software);
    int[] softArray = new int[softwareNum];

    String[] name = new String[softwareNum];
    int[] quantity = new int[softwareNum];

    // Initialize global variables
    SoftwareArray = new String[softwareNum];
    QuantityArray = new int[softwareNum];

    // loop through number of softwares
    for (int i = 0; i < softwareNum; i++) {
        System.out.println("Input name of software: ");
        String softwareName = userInput.readLine();

        name[i] = softwareName;

        System.out.println("Input quantity of software: ");
        String quantityString = userInput.readLine();
        int softwareQuantity = Integer.parseInt(quantityString);

        quantity[i] = softwareQuantity;

        // Copy the software name and quantity to the global variables
        QuantityArray[i] = quantity[i];
        SoftwareArray[i] = name[i];

        System.out.println("There are " + quantity[i] + " of the "
                + name[i] + " software.");
    }
}

// method to sort and display info
public static void displayInfo(int[] arr, String[] name) {
    // sort by quantity
    for (int i = 0; i < arr.length; i++) {
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[i] > arr[j]) {
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;

                String tempString = name[j];
                name[j] = name[i];
                name[i] = tempString;
            }
        }
        // output
        for (i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + "  " + name[i]);
        }
    }
}

// main
public static void main(String[] args) throws IOException {
    // input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    inputInfo();
    displayInfo(QuantityArray, SoftwareArray);
}
}
于 2013-08-27T18:03:48.523 回答