-1

我很难为我的程序创建排序方法。我创建了一个程序,询问用户是否要从 AZ 添加、删除、搜索或排序。这是我的程序代码

import java.util.Scanner;
import java.io.*;
/**
 * Write a description of class Final_Project here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Final_Project
{
    public static void main (String args[])throws IOException 
    {
        Scanner c = new Scanner(System.in);
        Scanner a = new Scanner(System.in);
        Scanner f = new Scanner(System.in);
        Scanner g = new Scanner(System.in);
        String car[][] = new String [20][4];
         System.out.println("JD Car Inventory");
         System.out.println();
        System.out.println();
         while (true)
        {
            System.out.println(); 
            System.out.println("What would you like to do?");
            System.out.println("a= Add vehicle");
            System.out.println("b= Delete vehicle");
            System.out.println("c= Search Inventory");
            System.out.println("d= Sort Inventory");
            System.out.println("e= Exit Program");
            System.out.println("f= View database");
            System.out.println();
            String get = g.nextLine();
            System.out.println();
             if (get.equalsIgnoreCase("a")){
                a(car);
            }
             else if(get.equalsIgnoreCase("b")){
                del(car);
            }
            else if (get.equalsIgnoreCase("c")){
                search(car);
            }
            else if (get.equalsIgnoreCase("d")){
                 sort(car);
            }
            else if (get.equalsIgnoreCase("e"))
            {   
                System.out.println ("Thank you for visiting JD CAR'S.");
                break;
            }
            else if (get.equalsIgnoreCase("f"))
            { 
                BufferedReader input = new BufferedReader (new FileReader("name.txt"));
                 for (int y=0;y<20;++y)
                {
                    for (int x=0;x<4;++x)
                    {   
                        String line = input.readLine();
                        car[y][x] = line;
                        if (line == null)
                        {
                            car[y][x] = "";
                        }
                    }
                }
                input.close();
                 PrintWriter output = new PrintWriter (new FileWriter("name.txt"));
                for (int y=0; y<20; y++) //rows
                {
                    for (int x=0; x<4; x++) //columns
                    {    
                        System.out.print (String.format("%1$-" + 32 + "s", car[y][x]));
                        output.println(car[y][x]);
                    }
                    System.out.println ();
                    System.out.println("------------------------------------------------------------------------------------------------------------------------");
                    //output.println();
                    //output.println("-------------------------------------------------------------------------------------");
                }
                output.close();
             }
            else 
            {
                System.out.println ("This is invalid. Enter again.");
            }
         }
     }  
    public static void a (String car[][])throws IOException
    {
        Scanner sc = new Scanner(System.in);
         for (int y=11;y<20;++y)
        {
            for (int x=0;x<4;++x)
            {   
                if (car[y][x].equals(""))
                {
                    while (true)
                    {    
                        System.out.println ("What is the name of the vehicle?");
                        car[y][x] = sc.nextLine ();
                         x+=1;
                        System.out.println ("How much Horsepower does it have?");
                        car[y][x]  = sc.nextLine ();
                         x+=1;
                        System.out.println ("What is the Engine Size (L)?");
                        car[y][x]  = sc.nextLine ();
                         x+=1;
                        System.out.println ("What is the price ($)?");
                        car[y][x] = sc.nextLine ();
                         x+=1;
                        System.out.println ("Would you like to add more?");
                        String add = sc.nextLine ();
                    }
                }                     
            }
         }
    }
     public static void del (String car[][])throws IOException
    {
        Scanner l = new Scanner(System.in);
        Scanner cs = new Scanner(System.in);
         System.out.println("Do you want to remove any vehicle?");
        String del = l.nextLine();
        if (del.equalsIgnoreCase("yes")){   
            System.out.println("Enter the field number you would like to remove (1,2,3,4)");
            double d= cs.nextDouble();
             System.out.println();
             if (d == 1){
                car[1][0] = "";
                car[1][1] = "";
                car[1][2] = "";
                car[1][3] = "";
            }
             else if (d == 2){
                car[2][0] = "";
                car[2][1] = "";
                car[2][2] = "";
                car[2][3] = "";
             }
             else if (d == 3){
                car[3][0] = "";
                car[3][1] = "";
                car[3][2] = "";
                car[3][3] = "";
            }
             else if (d == 4){
                car[4][0] = "";
                car[4][1] = "";
                car[4][2] = "";
                car[4][3] = "";
            }
             else if (d == 5){
                car[5][0] = "";
                car[5][1] = "";
                car[5][2] = "";
                car[5][3] = "";
                car[5][4] = "";
                car[5][5] = "";
            }
             else if (d == 6){
                car[6][0] = "";
                car[6][1] = "";
                car[6][2] = "";
                car[6][3] = "";
            }
             else if (d == 7){
                car[7][0] = "";
                car[7][1] = "";
                car[7][2] = "";
                car[7][3] = "";
            }
             else if (d == 8){
                car[8][0] = "";
                car[8][1] = "";
                car[8][2] = "";
                car[8][3] = "";
             }
            else if (d == 9){
                car[9][0] = "";
                car[9][1] = "";
                car[9][2] = "";
                car[9][3] = "";
            }
            else if (d == 10){
                car[10][0] = "";
                car[10][1] = "";
                car[10][2] = "";
                car[10][3] = "";
            }
         }
     }
     public static void search( String car[][])throws IOException
    {
         Scanner f = new Scanner(System.in);
        Scanner a = new Scanner(System.in);
        Scanner o = new Scanner(System.in);
         while(true){

            System.out.println ("0 = Car Name");
            System.out.println ("1 = Horsepower");
            System.out.println ("2 = Engine Size");
            System.out.println ("3 = Price");
            int cNumber = f.nextInt (); 
            System.out.println ("Enter what to search?");
            String sA = a.nextLine ();
            boolean flag=true;
            for (int j=11;j<20;++j)
            {
                for (int k=0;k<4;++k)
                {   
                    if(sA.equals (car[j][cNumber]))
                     {
                        while (true)
                        {  
                            flag=true;
                            int t=0; 
                            int q=0;
                             System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
                            q+=1;
                            System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
                            q+=1;
                            System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
                            q+=1;
                            System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
                            q+=1;
                            System.out.println();
                            break;
                        }
                    }
                }
                if (flag == false )
                    {
                        System.out.println();
                        System.out.println ("No search resulst for" + sA + ". Trying to see if you spelt the word right.");
                    }
                    System.out.println();
                    System.out.println ("Would you like to search again?");
                    sA = o.nextLine ();
                    if (sA.equalsIgnoreCase ("no"))
                    {
                        break;
                    }
                } 
            }
        }

    public static void sort (String car[][])throws IOException
    {

}


    }
 }

有人可以告诉我该怎么做,我应该使用冒泡排序吗?

4

2 回答 2

1

由于这看起来像研究,只有建议:

将问题拆分为已经完成(最初为空)和待做(最初全部)。然后反复从待办事项中挑选一个候选人并增加待办事项。

有一点像:

int sortedCars = 0;

while (sortedCars < car.length) {

    // Pick the smallest from the unsorted cars:
    int smallest  = sortedCars;
    for (int i = smallest + 1; i < car.length; i++) {
        if (car[i] less than car[smallest]) {
            smallest = i;
        }
    }

    // Exchange car[sortedCars] with car[smallest]:
    ...
    ++sortedCars;
}
于 2013-01-20T19:56:05.757 回答
0

我认为您不应该编写自己的排序方法。而是利用 Java 的内置Arrays.sort方法

例如。

Arrays.sort(car, new Comparator() {
    public int compare(Object o1, Object o2) {
        // compare based on which column you want
        return o1[0].compareTo(o2[0]);
    }
});
于 2013-01-20T19:29:10.697 回答