2017-08-14 72 views
0

我想将RestaurantSelector1类拉入CuisineChoice,这样我就可以让用户输入选择,然后我可以将其分配给RestaurantSelector1中的变量(如果这样做有道理。这是我有:从同一个包调用一个类并分配变量

在RestaurantSelector1:

public class RestaurantSelector1 { 

public static String getRandom(List<String[]> list) { 
Random random = new Random(); 
int listAccess = random.nextInt(list.size()); 
String[] s = list.get(listAccess); 
return s[random.nextInt(s.length)]; 


} 


public static void main(String[] args){ 
ArrayList<String[]> WestVillageCuisine = new ArrayList<String[]>(); 
WestVillageCuisine.add(Expensive.WestVillage.italian); 
. 
. 
. 
WestVillageCuisine.add(Expensive.WestVillage.greek); 

final String[] randomCuisine =  WestVillageCuisine.get(random.nextInt(WestVillageCuisine.size())); 
final String randomRestaurant = randomCuisine[random.nextInt(randomCuisine.length)]; 
final String[] randomWVItalian = WestVillageCuisine.get(0); 
final String westVillageItalian = r randomWVItalian[random.nextInt(randomWVItalian.length)]; 
System.out.println(getRandom(WestVillageCuisine)); 
System.out.println(randomRestaurant); 
System.out.println(westVillageItalian); 
} 

这是美食Choice类:

class CuisineChoice{ 
public static void main(String[] args){ 
RestaurantSelector1 A = new RestaurantSelector1(); 

System.out.println("What price range are you looking for? one = $-$$ and two = $$$-$$$$"); 
    Scanner scan2 = new Scanner(System.in); 
    String price = scan2.next(); 
    if (price.equals("two")){ 
     price = "Expensive"; 
    System.out.println("In which neighborhood would you like to eat?"); 
    Scanner scan = new Scanner(System.in); 
    String location = scan.next(); 
if (null != location)switch (location) { 
     case "Tribeca":{ 
      System.out.println("Great! And what type cuisine would you like in " + location + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location + " " + type + " " + price); 


      break; 

      } 
     case "West Village":{ 
      System.out.println("Great! And what type cuisine would you like in " + location + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location + " " + type + " " + price); 

       break; 
      } 
     case "Flatiron":{ 
      System.out.println("Great! And what type cuisine would you like in " + location + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location + " " + type + " " + price); 
       break; 
      } 
     case "Chelsea":{ 
      System.out.println("Great! And what type cuisine would you like in " + location + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location + " " + type + " " + price); 
       break; 
      } 
     default: 
      break; 
    } 
    } 
    else{ 
    price= "Inexpensive"; System.out.println("In which neighborhood would you like to eat?"); 
    Scanner scan3 = new Scanner(System.in); 
    String location1 = scan3.next(); 
if (null != location1)switch (location1) { 
     case "Tribeca":{ 
      System.out.println("Great! And what type cuisine would you like in " + location1 + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location1 + " " + type + " " + price); 
       break; 
      } 
     case "West Village":{ 
      System.out.println("Great! And what type cuisine would you like in " + location1 + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location1 + " " + type + " " + price); 
       break; 
      } 
     case "Flatiron":{ 
      System.out.println("Great! And what type cuisine would you like in " + location1 + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location1 + " " + type + " " + price); 
       break; 
      } 
     case "Chelsea":{ 
      System.out.println("Great! And what type cuisine would you like in " + location1 + " ?"); 
      Scanner scan1 = new Scanner(System.in); 
      String type = scan1.next(); 
      System.out.print(location1 + " " + type + " " + price); 
       break; 
      } 
     default: 
      break; 
    } 

} 
} 
} 

我只是无法弄清楚如何牛逼o使用位置和美食类型的用户输入作为输入,以获得该位置的该餐厅的随机餐厅。即使让RestaurantSelector1类出现在CuisineChoice中,我也遇到了麻烦。

任何帮助将不胜感激!

+1

我会强烈建议通过[Java教程](https://www.google.by/url?sa=t&rct=j&q=&esrc=s&source= web&cd = 1&cad = rja&uact = 8&ved = 0ahUKEwivhJShydfVAhXHBZoKHTdfDVwQFggmMAA&url = https%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2F&usg = AFQjCNE1m2PqVsncDnYixu6Kv67G70IpBw)。它完美地描述了您现在遇到的所有基础知识 –

回答

0

两者都有主要方法,哪一个是应用程序的起点,以及您的应用程序的结构是什么,他们是在同一个包还是不同的包中?而且你的代码中有一些类似if语句的错字,它没有“{”来打开语句,在case语句中,你不需要“{}”。

if (null != location)switch (location) { 

它应该是:

if (null != location){ switch (location) { 
+0

我希望将起始点作为您要求用户输入的地方。交换机和病例报告是在netbeans上向我推荐的,现在我将解决它们。 –

+0

我的意思是每个类都有main方法,并且应用程序只需要一个主要的方法来启动,或者至少指定它们作为应用程序的起点。在这个主要方法(应用程序的开始)中设置要求用户输入的代码。 – Omar