2014-12-04 36 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"); 
     } 
    } 
}