2013-01-20 80 views
-1

我很難爲我的程序創建排序方法。我創建了一個程序,如果想要添加,刪除,搜索或從A-Z排序,則會提醒用戶。 我的繼承人的程序有人可以幫助排序部分?

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 
    { 

} 


    } 
} 

代碼有人可以告訴我怎麼做,我應該使用冒泡排序?

+0

你有沒有在那裏得到了某種代碼和你問我們如何解決它,或者你問如何添加排序功能? – halfer

+0

另外請注意,你可以在你的'del'方法坍塌下來的一大塊代碼 - 用'車[d] [0] =「」;'然後你不必比較每個值1 ... 10(只做一次範圍檢查)。 – halfer

+0

我要求一個排序代碼。 –

回答

0

我不認爲你應該寫自己的排序方法。相反,利用Java的bulid在Arrays.sort方法

EG。

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]); 
    } 
}); 
+0

+1取消無法解釋的downvote。夥計們,總是給你一個讚譽的理由。 – halfer

1

由於這種貌似研究,唯一的忠告:

分割的問題已經完成(初始爲空),並(最初全部)。然後從待辦事項中重複挑選一名候選人並增加完成度。

如A位:

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