【问题标题】:Calling a class from same package and assigning variables从同一个包中调用一个类并分配变量
【发布时间】:2018-01-22 18:11:34
【问题描述】:

我正在尝试将 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);
}

这是美食选择课:

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

}
}
}

我只是不知道如何使用用户输入的位置和菜系类型作为输入来获取该位置的该菜系的随机餐厅。我什至根本无法让 RestaurantSelector1 类出现在 CuisineChoice 中。

任何帮助将不胜感激!

【问题讨论】:

  • 我强烈建议通过 Java tutorial 。它完美地描述了您现在遇到问题的所有基础知识

标签: java class arraylist random import


【解决方案1】:

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

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

应该是:

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

【讨论】:

  • 我希望起点是您要求用户输入的地方。在 netbeans 上向我建议了 switch 和 case 语句,我现在将修复它们。
  • 我的意思是每个类都有主方法,应用程序只需要一个主方法来启动,或者至少指定其中一个作为应用程序的起点。在这个 main 方法(应用程序的启动)中设置请求用户输入的代码。
猜你喜欢
  • 2015-08-30
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 2014-12-10
相关资源
最近更新 更多