Main不会调用数组

时间:2019-05-16 00:34:15

标签: java arrays random

我制作了一个数组以随机选择“牛排,比萨饼或薯条”,但是在运行程序时,它甚至在您提出问题之前就出错了。在给我一个例外之前,它可以处理大约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];
}

}

1 个答案:

答案 0 :(得分:0)

更改为

:: FoodArray ::

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

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

:: main ::

FoodArray.getRandomWord(args)

FoodArray.getRandomWord(FoodArray.strArray)

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