【问题标题】:Array and selection sort数组和选择排序
【发布时间】:2013-08-30 07:51:01
【问题描述】:

我的任务要求我编写一个简单的 Java 程序来盘点计算机软件。设计一个程序,让用户输入软件名称和库存数量。

然后程序应该对项目进行排序,以便记录按库存数量排序,并按数字顺序显示所有记录。

我必须使用两种方法!一种获取用户输入并存储在数组中的方法,以及一种按从小到大排序并显示的方法!

这是我的代码。我不确定下一步该怎么做。我不太确定如何使用选择排序对其进行排序,但这正是我需要使用的。那里的那个是我从一堂课中使用的!我确定 displayInfo 方法是错误的,因为我不知道如何初始化任何尚未在 inputInfo 方法中使用的变量(我需要数组和东西)。当我在 main 中调用 displayInfo 方法时它不起作用。

我希望这是有道理的。不知道怎么解释...

请帮忙!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; 
import java.util.ArrayList;
import java.util.Arrays;

public class ComputersRUs {
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];       

    //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;

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

//method to sort and display info
public static void displayInfo(int[] arr)
{       
    //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;
            }
        }
        //output
        for(i=0; i<=arr.length; i++)
        {
            System.out.println(arr[i] + "  ");
        }
    }
}

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

    //loop to stop when user requests
    String quit = "n";
    while("n".equals(quit))
    {
        //display menu
        System.out.println("'COMPUTERS R US' Software Inventory - Main Menu");
        System.out.println("1) Input information");
        System.out.println("2) Display information in order");      
        System.out.println("3) Quit program");
        System.out.print("Please choose an option by inputting the number of your choice: ");
        String choiceString = br.readLine();
        int choice = Integer.parseInt(choiceString);

        if(choice == 1)
        { 
        inputInfo();
        }else if(choice == 2)
        { 
        displayInfo();
        }else if(choice == 3)
        { 
            System.out.println("Are you sure you want to quit? (y/n) ");  
            quit = br.readLine();
        }else
        { 
            System.out.println("Not a valid option."); 
        }                   
    }
}
}

谢谢!

【问题讨论】:

  • 请看看如何提出家庭作业问题 (meta.stackexchange.com/questions/10811/…)。您的问题相当笼统,您应该更加努力地解决问题。 displayInfo 似乎是某种冒泡排序。如果你必须使用选择排序,你应该先看看算法然后实现它:基本上你有两个集合(排序和未排序),排序开始时是空的。然后,您将选择未排序集中的最小元素并将其附加到已排序集中。对每个元素都这样做。

标签: java arrays sorting variable-assignment


【解决方案1】:

为了帮助您解决排序问题,我建议您阅读

http://www.leepoint.net/notes-java/data/arrays/70sorting.html

至于初始化数组,为什么不使用

将数组发送到 displayInfo
displayInfo(softArray);

然后在displayInfo中

public static void displayInfo(int[] arr){
int[] newSoftArray = arr; 

然后您可以使用返回方法返回数组

【讨论】:

    猜你喜欢
    • 2016-08-15
    • 2023-03-15
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多