2014-12-04 38 views
-3

我試圖從陣列致電值打印(在Java中,我使用的Eclipse)有誰知道如何從數組中調用值?

  • 用戶呈現其中有10個目的地列表例如第一屏(1)西班牙(2)法國
  • 選擇所述用戶呈現的10家列表中的目的地之後
  • 後呈現所需的週數的賓館(保持簡單1,2或3周) *選擇宴會號碼的持續時間後,12歲及以下的人數(每人減少50%)以及多少13+全價乘客
  • 完成上述操作後,會在屏幕上顯示完整分類並且會給出書本選項
  • 系統必須處於循環狀態,因此可以整天執行訂單,但是在任何時候輸入-99都會結束應用程序。

代碼:

import java.util.Scanner; 
public class TravelAgent 
{ 
    public static void main(String [] args) 
    { 

     Scanner scan = new Scanner(System.in); 
     Scanner keyboard = new Scanner(System.in); 

     int country_choice; 
     int country_prices; 
     int user_selecction; 
     int country_length; 
     int countrylength; 
     int x; 

     String[] country = 

      { 
       "Greece","Spain","France","Russia","Italy","Mexico","Germany","Maldives","Thailand","Portugal" 
      }; 

     int [] country_prices_1 = 

      { 
       300,400,200,500,250,800,450,900,750,350 
      }; 

     String[] hotels_greece = 
      { 
       "Lindos Blu","Saint Andrea","The Excelsior","Elite Suites","Avithos","New Hotel","The Met Hotel", 
       "TesoroBlu Hotel & Spa","Mykonos Grand Hotel","Achtis Hotel" 
      }; 

     String[] hotels_spain = 
      { 
       "Alma Barcelona", "Seaside Grand Hotel Residencia", "Hotel Primero Perimera", "Valbusenda Resort", 
       "Hotel Maria Cristina", "Gran Hotel La Perla", "El Palace Hotel", 
       "Tinas de Pechon", "Dream Hotel Gran Tacande", "Protur Palmeras Playa" 
      }; 

     String[] hotels_france = 
      { 
       "Le Bristol Paris", "Hotel Plaza Athenee","Hotel Fabric","La Maison Favart","Hotel Le Six", 
       "Hotel Royal Riviera", "Hotel Crillon Le Brave", "Mandarin Oriental", "Le Mareuil", "Hotel d’Europe" 
      }; 

     String[] hotels_russia = 
      { 
       "Lotte Hotel Moscow", "Old Estate Hotel & SPA","Pushka Inn Hotel","The Ritz-Carlton Moscow","Mercure Arbat Moscow", 
       "Belmond Grand Hotel Europe", "Ararat Park Hyatt Moscow", ".Astoria Hotel", "Helvetia Hotel", "Katerina City Hotel" 
      }; 
     String[] hotels_italy = 
      { 
       "Hotel Monika", "Bellevue Syrene","Hotel Ai Reali","Hotel Belvedere","Hotel Buca di Bacco", 
       "Petronilla Hotel", "Santa Caterina Hotel", "Color Hotel", "Belmond Grand Hotel Timeo", "Antiche Mura Hotel" 
      }; 

     String[] hotels_mexico = 
      { 
       "Rosewood Mayakoba", "Hotel Jashita","Villa La Estancia","Capella Ixtapa","Banyan Tree Cabo Marques", 
       "The Beloved Hotel", "Casa Misha", "Esperanza", "Excellence Playa Mujeres", "La Casa Que Canta" 
      }; 

     String[] hotels_germany = 
      { 
       "Swissotel Dresden", "Mandarin Oriental","Park Hyatt Hamburg","Steigenberger Grandhotel Handelshof", 
       "Hotel Villa Hugel", "Sonnenalp Resort", "Hezelhof Hotel", "Das Stue", "Gutshaus Stolpe", "Hotel Edelweiss" 
      }; 

     String[] hotels_maldives = 
      { 
       "Baros Maldives","Soneva Fushi Resort","Constance Moofushi","Taj Exotica Resort & Spa","Mirihi Island Resort", 
       "Lily Beach Resort & Spa","Naladhu Resort","Veligandu Island Resort","Cocoa Island Resort","Six Senses Laamu" 
      }; 

     String[] hotels_thailand = 
      { 
       "Dhara Devi Chiang Mai","Oriental Residence","Layana Resort & Spa","Beyond Resort Khaolak","Sunsuri Phuket", 
       "Rimping Village","Rabbit Resort","The Siam","The Peninsula",".Pimalai Resort & Spa" 

      }; 

     String[] hotels_portugal = 
      { 
       "The Cliff Bay","Conrad Algarve","Bristania Hotel","Olissippo Lapa Palace","Quinta Jardins de Lago","The Yeatman", 
       "Altis Belem Hotel & Spa","Quinta da Bela Vista","Hotel Santa Justa","Hotel Belavista da Luz" 
      }; 

     int [] hotel_prices_greece = 
      { 
       70,90,100,140,80,75,200,110,160,230 
      }; 

     int [] hotel_prices_spain = 
      { 
       50,70,90,100,120,140,160,180,200,230 
      }; 
     int [] hotel_prices_france = 
      { 
       60,60,70,75,80,90,105,120,140,200 
      }; 
     int [] hotel_prices_russia = 
      { 
       80,90,90,95,100,110,130,150,175,190 
      }; 
     int [] hotel_prices_italy = 
      { 
       80,85,90,100,115,130,140,220,235,240 
      }; 
     int [] hotel_prices_mexico = 
      { 
       200,240,260,275,300,360,375,380,395,450 
      }; 
     int [] hotel_prices_germany = 
      { 
       60,65,70,85,100,120,150,180,220,210 
      }; 
     int [] hotel_prices_maldives = 
      { 
       260,280,300,350,380,400,440,450,550,580 
      }; 
     int [] hotel_prices_thailand = 
      { 
       245,255,270,290,300,350,365,370,390,400 
      }; 
     int [] hotel_prices_portugal = 
      { 
       110,115,120,120,140,150,180,185,250,260 
      }; 
     System.out.println("please select your holiday destination"); 
     System.out.println("************************************"); 
     System.out.println("*  Want to book a holiday?  *"); 
     System.out.println("*         *"); 
     System.out.println("*  You're at the right place *"); 
     System.out.println("*         *"); 
     System.out.println("************************************"); 
     System.out.println("* Please choose from the following *"); 
     System.out.println("*   1.Greece    *"); 
     System.out.println("*   2.Spain    *"); 
     System.out.println("*   3.France    *"); 
     System.out.println("*   4.Russia    *"); 
     System.out.println("*   5.Italy    *"); 
     System.out.println("*   6.Mexico    *"); 
     System.out.println("*   7.Germany    *"); 
     System.out.println("*   8.Maldives    *"); 
     System.out.println("*   9.Thailand    *"); 
     System.out.println("*   10.Portugal    *"); 
     System.out.println("** Enter Destination from 1-10 **"); 

     country_choice =keyboard.nextInt(); 

     For (int x = 0; x < country.price; x++) { 

     } 

     //country_choice = Integer.parseInt(user_selection.nextLine())-1; 

     country_choice = Integer.parseInt(user_selection.nextLine())-1; 

     System.out.println("country[x]" + "country_prices[x]"); 



     //「+ country [country_choice]+ country prices [country_prices]+」.00」); 

    } 

} 
+2

刪除引號從數組訪問; ''country [x]「+」country_prices [x]「'應該是'country [x] + country_prices [x]'。只要其中一種類型是'String',那麼串聯就可以工作。 – Makoto 2014-12-04 22:37:57

+1

你爲什麼使用這麼多的數組?難道你不能創建一個酒店對象並擁有一系列酒店嗎? – 2014-12-04 22:39:09

+0

你卡在哪裏?從你的文章中不清楚你實際上在問什麼。 – Brian 2014-12-04 22:45:34

回答

0

在我們開始之前:

  • 就像在評論中說,你應該聲明一些類和輔助方法,但我會假設你還沒有達到這一點,所以我會盡一切在main
  • 在Java中的約定是使用駝峯,但由於您使用的是snake_case我也將在示例中使用它。
  • 爲了使示例更加簡潔,我將其縮減爲3個國家。

而不是每個國家的酒店和酒店價格都有一個單獨的變量,您應該使用數組數組(數組中的每個條目是另一個數組)。例如,法國是數字3,這意味着它的索引是2(數組索引從0開始),因此hotels[2]將是法國酒店名稱的數組(而不是hotels_france)。這樣,如果您在變量中包含國家/地區號碼,則可以輕鬆訪問該國家/地區的酒店列表,而無需單獨爲每個國家/地區編碼。

它是這樣的:

import java.util.Scanner; 

public class TravelAgent { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 
     Scanner keyboard = new Scanner(System.in); 

     String[] countries = { 
      "Greece", "Spain", "France" 
     }; 

     int[] country_prices = { 
      300, 400, 200 
     }; 

     String[][] hotels = new String[3][]; 

     hotels[0] = new String[]{ 
      "Lindos Blu", "Saint Andrea", "The Excelsior", "Elite Suites", "Avithos", "New Hotel", "The Met Hotel", 
      "TesoroBlu Hotel & Spa", "Mykonos Grand Hotel", "Achtis Hotel" 
     }; 
     hotels[1] = new String[]{ 
      "Alma Barcelona", "Seaside Grand Hotel Residencia", "Hotel Primero Perimera", "Valbusenda Resort", 
      "Hotel Maria Cristina", "Gran Hotel La Perla", "El Palace Hotel", 
      "Tinas de Pechon", "Dream Hotel Gran Tacande", "Protur Palmeras Playa" 
     }; 
     hotels[2] = new String[]{ 
      "Le Bristol Paris", "Hotel Plaza Athenee", "Hotel Fabric", "La Maison Favart", "Hotel Le Six", 
      "Hotel Royal Riviera", "Hotel Crillon Le Brave", "Mandarin Oriental", "Le Mareuil", "Hotel d’Europe" 
     }; 

     int[][] hotel_prices = new int[3][]; 
     hotel_prices[0] = new int[]{ 
      70, 90, 100, 140, 80, 75, 200, 110, 160, 230 
     }; 
     hotel_prices[1] = new int[]{ 
      50, 70, 90, 100, 120, 140, 160, 180, 200, 230 
     }; 
     hotel_prices[2] = new int[]{ 
      60, 60, 70, 75, 80, 90, 105, 120, 140, 200 
     }; 

     while (true) { 
      System.out.println("Please select your holiday destination"); 
      for (int i = 0; i < countries.length; ++i) { 
       System.out.println((i + 1) + "." + countries[i]); 
      } 
      System.out.println("Enter destination from 1-" + countries.length); 

      int country_choice = keyboard.nextInt(); 
      if (-99 == country_choice) { 
       return; 
      } 
      System.out.println("Chosen country: " + countries[country_choice - 1]); 
      System.out.println("Country price: " + country_prices[country_choice - 1] + ".00"); 


      System.out.println("Please select your hotel"); 
      for (int i = 0; i < hotels[country_choice - 1].length; ++i) { 
       System.out.println((i + 1) + "." + hotels[country_choice - 1][i]); 
      } 
      System.out.println("Enter hotel from 1-" + hotels[country_choice - 1].length); 

      int hotel_choice = keyboard.nextInt(); 
      if (-99 == hotel_choice) { 
       return; 
      } 
      System.out.println("Chosen hotel: " + hotels[country_choice - 1][hotel_choice - 1]); 
      System.out.println("Hotel price: " + hotel_prices[country_choice - 1][hotel_choice - 1] + ".00"); 
     } 
    } 
}