我应该做的是编写一个程序,让用户输入软件名称和库存数量。我需要将它们存储在一个数组中,然后使用选择排序按照从最少到最多的顺序对其进行排序。
我的问题是,我不想把软件的名字和数字分开!此外,当我编译时它没有显示排序的名称!我读过关于选择排序的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.
然后什么都没有。它根本不显示排序列表。