gpt4 book ai didi

java - 如何让我的代码从食品店随机生成的数字中进行选择?

转载 作者:行者123 更新时间:2023-12-01 16:49:50 26 4
gpt4 key购买 nike

当我按下按钮时,我希望它从 7 个亚洲地点之一中随机选择美食地点。当我的随机数为: 2 时,我会打印出

"Today you will eat at null".

如何让它打印出餐厅的名称并将其链接到新的 java 页面以显示其信息?

这是我声明数组的地方:

 private void createResArray() {
Restaurant chacha = new Restaurant("Cha Cha", FoodCategory.Asian);
Restaurant sammy = new Restaurant("Sammy Chom's K-Town", FoodCategory.Asian);
Restaurant sushianddinsum = new Restaurant("Sushi & Dinsum", FoodCategory.Asian);
Restaurant masterwok = new Restaurant("Master Wok", FoodCategory.Asian);
Restaurant chinastar = new Restaurant("China Star", FoodCategory.Asian);
Restaurant phohasaigon = new Restaurant("Pho Ha Saigon", FoodCategory.Asian);


Restaurant taiAli = new Restaurant("Tai’s Ali's Middle East Food", FoodCategory.Middle_Eastern);
Restaurant aliMiddle = new Restaurant("Ali's Middle East Food", FoodCategory.Middle_Eastern);
Restaurant nycHalal = new Restaurant("NYC Halal", FoodCategory.Middle_Eastern);

resArray = new SparseArray<Restaurant>();

resArray.append(0, chacha);
resArray.append(1, sammy);
resArray.append(2, sushianddinsum);
resArray.append(3, masterwok);
resArray.append(4, chinastar);
resArray.append(5, phohasaigon);


resArray.append(6, taiAli);
resArray.append(7, aliMiddle);
resArray.append(8, nycHalal);
resCount = 9;

这是我的随机数生成器:

 private void pickRestaurant(int checkedRadioButtonId) {
Random r = new Random();
int randomNumber = -1;

switch (checkedRadioButtonId) {
case R.id.Asian:
randomNumber = r.nextInt(7)+1 ;
break;
case R.id.middle_eastern:
randomNumber = r.nextInt() ;
break;

}

    String randomRestaurantName = resArray.get(randomNumber).getName();
Toast toast = null;
if (randomNumber < 0) {
toast = Toast.makeText(mContext, "Select from one of the following:",
Toast.LENGTH_SHORT);
} else {
toast = Toast.makeText(mContext, "Today you will eat at "
+ randomRestaurantName , Toast.LENGTH_LONG);
}
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

}

Resturant.java

public class Restaurant {
private String name;
FoodCategory foodCategory;
public String getName;

public Restaurant(String name, FoodCategory priceRange) {
this.name = name;
this.foodCategory = priceRange;
}

public String getName()
{
return getName;
}

protected FoodCategory getFoodCategory()
{
return foodCategory;
}

}

最佳答案

您需要重写 Restaurant 类中的 toString() 方法,使其返回更合理的内容。

关于java - 如何让我的代码从食品店随机生成的数字中进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42688347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com