【问题标题】:Main will not call arrayMain 不会调用数组
【发布时间】:2019-05-16 00:34:14
【问题描述】:

我创建了一个数组来随机选择“牛排、披萨或薯条”,但我在运行我的程序时,甚至在你回答问题之前就出错了。在给我一个例外之前,它适用于大约 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++;
    }

    if (input == 3) {
        System.out.println("Trick question, you already are invisible loser."
                + "Lets continue. ");
        savory++;

    }

    System.out.println("Okay. now heres a random suggestion.");
  {System.out.println(FoodArray.getRandomWord(args));
}


    System.out.println("Okay. Lets see your final score."
            + "Press 0 to continue");

    input = myscanner.nextInt();

    if (sweet > salty && sweet > savory) {
        System.out.println("Get something sweet. maybe cake.");
    }

    if (salty > sweet && salty > savory) {
        System.out.println("Get something salty. Maybe potatoe chips");

    }

    if (savory > sweet && savory > salty) {
        System.out.println("Savory! Steak steak steak!");
    }

    if (salty == sweet && salty == savory) {
        System.out.println("Its a tie. Can't help you. sorry.");
    }

    if (sweet == salty && sweet == savory) {
        System.out.println("Its a tie. Can't help you. sorry.");
    }

    if (savory == sweet && savory == salty) {
        System.out.println("Its a tie. Can't help you. sorry.");
    }

}

}



public class FoodArray {




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


static public String getRandomWord(String[] array) {
Random r = new Random();
int index = r.nextInt(array.length);
return array[index];
}

}

【问题讨论】:

  • public class FoodArray { String[] strArray = { "Pizza", "Steak", "Chips" };静态字符串 getRandomWord(String[] array) { 随机 r = new Random(); int index = r.nextInt(array.length);返回数组[索引];
  • 以上是我的数组类,由于某种原因,该网站不允许我将数组类与整个代码一起发布。我收到了这些异常:线程“main”中的异常 java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Random.java:388) at quiz3.FoodArray.getRandomWord(FoodArray.java:19)在 quiz3.Quiz3.main(Quiz3.java:94)
  • 您正在将 java 程序运行时参数 args 传递给此方法。也许你的意思是使用strArray
  • 我试过了,我得到一个“找不到符号”的错误。我不知道为什么。
  • 是的,strArray 在你的FoodArray 类中,所以在main 中找不到,所以问题是,你真的需要向这个方法传递任何东西吗?跨度>

标签: java arrays random


【解决方案1】:

改成

::FoodArray::

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

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

::main::

FoodArray.getRandomWord(args)

FoodArray.getRandomWord(FoodArray.strArray)

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

【讨论】:

    猜你喜欢
    • 2020-11-23
    • 2014-09-10
    • 1970-01-01
    • 2020-02-13
    • 2018-02-05
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2022-12-31
    相关资源
    最近更新 更多