-5

I'm trying to write a program that creates random spell effects, but 'length' cannot be resolved to a variable...

package Chaos;

import java.util.Random;

public class Effects {
    public static void main(String[] args) {

        String[] objects = { "Effect1", "Effect2", "Effect3", "Effect4" };

        int effect = objects.length;

        for (int i = 0; 1 < length; i++) {
            int rand = (int) (Math.random() * length);
            System.out.print(objects[rand]);
            System.out.print(" ");
        }
    }
}
4

2 回答 2

6

您将变量声明为effect,而不是length

int effect = objects.length;

for (int i = 0; 1 < length; i++) // <-- You are using the wrong variable name here
于 2013-04-04T15:48:59.707 回答
0

在您的代码中进行以下更改:

       int effect = objects.length;

    for (int i = 0; i < effect ; i++) {

或者

String[] objects = { "Effect1", "Effect2", "Effect3", "Effect4" };

    for (int i = 0; i < objects.length; i++) {
于 2013-04-04T15:59:33.360 回答