为了账号安全,请及时绑定邮箱和手机立即绑定

Main 不会调用数组

Main 不会调用数组

aluckdog 2022-11-30 13:19:36
我制作了一个数组来随机选择“牛排、比萨饼或薯条”,但在运行我的程序时,它在你回答问题之前就出错了。在给我一个例外之前,它适用于大约 5 个输入。我认为问题出在{System.out.println(FoodArray.getRandomWord(args));但我似乎无法弄清楚它有什么问题。我只需要我的程序能够正确运行,当它遇到关于给出随机建议的问题时,它随机打印出牛排、比萨饼或薯条。public static void main(String[] args) {    int input = 0;    int sweet = 0;    int savory = 0;    int salty = 0;    Random r = new Random();    FoodArray a = new FoodArray();    Scanner myscanner = new Scanner(System.in);    System.out.println("Welcome, lets figure out what you're hungry for."            + "Press 0 to continue");    input = myscanner.nextInt();    if (input > 0) {    }    {        System.out.println("What sounds the best to you right now?\n"                + "1) Something sweet\n"                + "2) Savory\n "                + "3) Salty\n ");    }    input = myscanner.nextInt();    if (input == 1) {        System.out.println("something sweet. okay. next question");        sweet++;    } else if (input == 2) {        System.out.println("Savory eh? got it. lets move on.");        salty++;    } else if (input == 3) {        System.out.println("Sounds good. Lets proceed");        savory++;    }    System.out.println("Press 0 to continue");    input = myscanner.nextInt();    System.out.println("pick a number 1-3");    input = myscanner.nextInt();    if (input == 1) {        System.out.println("Very interesting.");        sweet++;    } else if (input == 2) {        System.out.println("Very interesting.");        salty++;    } else if (input == 3) {        System.out.println("Very interesting.");        savory++;    }    System.out.println("Press 0 to continue");    input = myscanner.nextInt();    System.out.println("Next question: "            + "Would you rather, fly, (1) breath underwater, (2) or be invisible (3)");    input = myscanner.nextInt();    if (input == 1) {        System.out.println("Good choice.");        sweet++;    }    if (input == 2) {        System.out.println("Would be pretty cool");        salty++;    }
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

改成

::食品阵列::

String[] strArray = { "Pizza", "Steak", "Chips" };

public static String[] strArray = { "Pizza", "Steak", "Chips" };

::主要的::

FoodArray.getRandomWord(args)

FoodArray.getRandomWord(FoodArray.strArray)

但这是愚蠢的,因为FoodArray已经知道strArray所以删除参数传递给这个方法并strArray在其中使用FoodArray


查看完整回答
反对 回复 2022-11-30
  • 1 回答
  • 0 关注
  • 62 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信