全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2309
推到 Plurk!
推到 Facebook!

一個Array 程式,sorting有問題

尚未結案
sysesame
一般會員


發表:1
回覆:2
積分:0
註冊:2005-03-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-04 08:08:05 IP:24.69.xxx.xxx 未訂閱
我是電腦新手, 最近修了一個電腦課, 真是一個頭兩個大啊... 現在教到JAVA的ARRAY 和 SORTING, 有點搞不太清楚...     內容是,要寫一個CLASS 可以把餐廳的四樣基本資料存進ARRAY 裡: 1. 名字 (String) 2. 料理 (String, 例如: Japanese) 3. 評分1-4 (int) 4. 價位 $-$$$$$ (String)    然後, 這些基本資料會在一個"restaurant.dat"的檔裡, 每行以 "McDonald's/FastFood/1/$" 的形式... 我大概知道讀取檔案是用 Scanner fileScan = new Scanner (new File ("Restaurant.dat")); 來執行.. 但是搞不太清楚要怎麼把裡面的東西放進ARRAY... 還有, 放了之後, 要讓使用者能夠依照喜好尋找想要的價位或某種料理的餐廳, 問完還要幫使用者把艘詢出來的資料依照餐廳的名字排列或者依照價位排列... 很麻煩啊... 這個地方不懂的是, 如果問使用者想找的餐廳的價位, 讓他們輸入 (1-4), 那要怎麼跟 $$$$ 比對?? (例如, 如果輸入3, 那要怎麼找到3個錢??) 要是有人可以幫忙一下, 感激不盡啦!!! 反正, 結果要如下面的: Example 1: Welcome to the Restaurant Finder Initializing restaurant database Initialization successful What kind of search would you like to do? 1 = search restaurants by cuisine 2 = search restaurants by price 9 = exit program Enter choice: 2 Search by price: Enter lower bound on price (1-5): 2 Enter upper bound on price (1-5): 3 What kind of sort would you like to do? 1 = sort restaurants by name 2 = sort restaurants by price 9 = no sorting Enter choice: 1 Search results -- sorted by name: Andale's Mexican 2 $$ Earl's Generic 3 $$$ Ginger and Chili Chinese 3 $$ Las Margaritas Mexican 3 $$$ Pepitas Mexican 3 $$ York Pizza Pizza 3 $$ 第一個CLASS (METHODS):
public class Restaurant
{
  private String resName, resCuisine, resPrice;
  private int resQuality;      //constructor method for creating a restaurant profile
  public Restaurant (String Name, String Cuisine, int Quality, String Price)
  {
    resName = Name;
    resCuisine = Cuisine;
    resPrice = Price;
    resQuality = Quality;
  }
  
  //method to get restaurant name
  public String getName()
  {
    return resName;
  }
    
  //method to get cuisine type  
  public String getCuisine()
  {
    return resCuisine;
  }
  
  //method to get quality rating
  public int getQuality()
  {
    return resQuality;
  }
  
  //method to get price rating
  public String getPrice()
  {
    return resPrice;
  }
  
  //the toString method
  public String toString()
  {
    String report = "Restaurant Profile\n-------------------------------\n";
    report  = "\nRestaurant Name: "   resName;
    report  = "\nCuisine Type: "   resCuisine;
    report  = "\nQuality Rating: "   resQuality;
    report  = "\nPrice Range: "   resPrice;
    return report;
  }
}
第二個CLASS:
import java.util.Scanner;    public class RestaurantList
{
  private String resName, resCuisine, resPrice;
  private int resQuality;
  
  Restaurant [] resList = new Restaurant[100];
  int count = 0;
  
  //adds restaurant input to an index in array
  public void addRestaurant(String resName, String resCuisine, int resQuality, String resPrice)
  {
    if (count == resList.length)
      increaseSize();
    
    resList[count] = new Restaurant (resName, resCuisine, resQuality, resPrice);
    count  ;
  }
  
  //increases size of array when space is not enough
  public void increaseSize()
  {
    Restaurant[] temp = new Restaurant[resList.length   100];
    for (int res = 0; res < resList.length; res  )
      temp[res] = resList[res];
    resList = temp;
  }
  
  //outputs the restaurant list (information)
  public String printRestaurants()
  {
    System.out.println("Restaurants Listings\n-------------------------------------\n");
    for (count = 0; count <= resList.length; count  )
      System.out.println(resList[count]);
    
    String end = "end of list.";
    return end;
  }      //outputs the restaurant profile when an index is entered
  public String getRestaurant()
  {
    Scanner kbd = new Scanner (System.in);
    System.out.println("Please enter an integer as index number: ");
    int index = kbd.nextInt();
    System.out.println(resList[index]);
    String input = "(Index "   index   ")";
    return input;
  }
  
  
}
這裡是程式, 也不知道對不對:
import java.util.Scanner;
import java.io.*;    public class RestaurantFinder
{
 public static void main (String[] args) throws IOException
 {
   //***************************************************
   // Declare variables here
   //       int maxNum = 100;
   int resCategories = 4;
   String [][] resList = new String [maxNum][resCategories]; 
   String [][] tempList = new String [maxNum][resCategories];
   
   System.out.println("Welcome to the Restaurant Finder");
     
   //***************************************************
   // Step one:
   // Construct a loop here that initializes the a 
   // RestaurantList object by doing the following:
   // Read a restaurant record from "restaurant.dat"
   // Parse restaurant data into components
   // Create new restaurant object from components
   // Add the new restaurant object to the next empty
   // location in the RestaurantList object
   //
   // For the purpose of this explanation, we'll call this 
   // RestaurantList object "array1", but you should use a 
   // more meaningful name.
   
   System.out.println("Initializing Restaurant Database...");
   Scanner fileScan = new Scanner (new File ("Restaurant.dat"));
   while (fileScan.hasNext())
   {
     這個部分很有問題
     String temp1 = fileScan.next();
     String temp2 = fileScan.next();
     int temp3 = fileScan.nextInt();
     String temp4 = fileScan.next();
     Restaurant tempRes = new Restaurant (temp1, temp2, temp3, temp4);
     int index = 0;
     resList[index] = tempRes.addRestaurant(); <----這段讀不到? 為什麼?? 一值顯示cannot find symbol
     index  ;
   }
   System.out.println("Initialization Successful.");
   
   //***************************************************
   // Step two:
   // Get input from user about the search to be performed
   // then search through array1 for restaurants
   // meeting the user's criteria. This search requires 
   // another loop. Add each restaurant that meets the 
   // criteria to another object of type RestaurantList. 
   // We'll call this array "array2" in this explanation, but
   // again you should use a more meaningful name. This object,
   // array2, will contain only those restaurants that satisfy
   // the search criteria. 
   
   int choice, lowerPrice, upperPrice;
   String cuisineInput;
   int number = 0;
   Scanner kbd = new Scanner(System.in);
   
   // Ask user for input here
   
   while (number == 0)
    {
      System.out.println ("\n\n\nWhat kind of search would you like to do?\n(please enter a command number)" 
                              "1 = Search restaurants by cuisine\n2 = Search restaurants by price\n3 = Exit Program"
                              "\nEnter Choice: "); 
      choice = kbd.nextInt();
   
      // Put your code for performing the search here
      
      if (choice == 1)
      {
        System.out.print("Search by cuisine:\nEnter cuisine type: ");
        cuisineInput = kbd.next();
        
        for (int i = 0; i <= (resList.length - 1); i  )
        {
          if (resList[i][2].equalsIgnoreCase (cuisineInput))
          {
            for (int j = 0; j < 4; j  )
              tempList[i][j] = resList[i][j];
          }
        }
      }
      
      else if (choice == 2)
      {
          System.out.print("\n\nSearch by price:\nEnter lower bound on price (1-5): ");
          lowerPrice = kbd.nextInt();
          System.out.print("\nEnter upper bound on price (1-5): ");
          upperPrice = kbd.nextInt();
          if (lowerPrice > 5 || upperPrice > 5 || lowerPrice <= 0 || upperPrice <= 0)
          {
            System.out.println ("\nInput is out of bound. Please re-try.");
            number = 10;
          }
          else if (lowerPrice > upperPrice)
          {
            System.out.println ("\nUpper bound is less than lower bound. Please re-try.");
            number = 10;
          }
      }
        
      else if (choice == 3)
        number = 10; 
      
      else
        System.out.println ("Please enter a valid choice");       
   //***************************************************
   // Step 3:
   // Get input from user about the sorting to be performed.
   // Then use a nested loop to re-order the restaurants in
   // array2 and place them in sorted order in array3, which
   // is also of type RestaurantList (and whose name should also
   // be more meaningful). 
   
   // Ask user for input here
      
      int num = 0;
      while (num < 3)
      {
        System.out.print ("\n\nWhat kind of sort would you like to do?\n1 = Sort restaurants by name"
                            "\n2 = Sort restaurants by price\n9 = No sorting\nEnter choice: ");
        int sortChoice = kbd.nextInt();
      
   // Put your code for performing the sorting here
      
        String [][] sortingList = new String [maxNum][resCategories];
        if (sortChoice == 1)
        {
          for (int i = 0; i <= (tempList.length - 1); i  )
          {
            未完.. 卡住不知道怎麼繼續
          }
          num = 10; number = 10;
        }
   
        else if (sortChoice == 2)
        {
          這裡要用價位排列, 根本不知道怎麼開始
          num = 10; number = 10;
        }
      
   // If no sorting was done, print the contents of array2.
   // Otherwise, print the contents of array3.
      
        else if (sortChoice == 9)
        {
          for (int i = 0; i <= (tempList.length - 1); i  )
          {
            for (int j = 0; j < 4; j  )
              System.out.print (tempList[i][j]);
          }
          num = 10; number = 10;
        }
   
        else
        {
          System.out.println ("That was not a valid sorting method. Please re-try.");
          num  ;
        }
      }
            
  
  
  
  
  
  
  
   }
   
   System.out.println("Thank You For Using Restaurant Finder.");
  
  
 }
}
發表人 - sysesame 於 2005/03/04 12:04:03 發表人 - sysesame 於 2005/03/04 12:05:33
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-04 11:35:56 IP:61.64.xxx.xxx 未訂閱
麻煩把你的code用 [code ] ... [/code ] 掛縮要做好.不然很難debug.
sysesame
一般會員


發表:1
回覆:2
積分:0
註冊:2005-03-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-04 12:13:47 IP:24.81.xxx.xxx 未訂閱
修正好了.. 麻煩幫我看看.. 感激... 有看到其他帖有問關於 sort() 的事, 但是機車的是, 老師規定只能以第三個ARRAY來做sorting... 即: 只能把搜尋到符合的從第一個 ARRAY 拿出來, 放在第二個暫時的 ARRAY, 然後在整理順序的時候, 把應該放第一位的從第二個ARRAY拿出來放在第三個ARRAY, 然後把第二個ARRAY裡的紀錄刪掉... 再找第二順位的餐廳, 放在第三個ARRAY的第二個位置, 然後把第二個ARRAY的紀錄刪掉... 諸如此類... 要怎麼找順序, 完全搞不懂...
sysesame
一般會員


發表:1
回覆:2
積分:0
註冊:2005-03-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-03-06 00:29:12 IP:24.81.xxx.xxx 未訂閱
這個.. 不用解了... 我自己弄好了 @@"
alfarene
一般會員


發表:6
回覆:7
積分:2
註冊:2004-10-14

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-03-11 10:03:56 IP:211.72.xxx.xxx 未訂閱
您好,恰巧我也碰到類似的問題,可以分享一下您的解決方法嗎? 方便也POST一下您的source code嗎? 謝謝!!
系統時間:2024-04-27 22:48:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!