无法解析变量?

时间:2013-04-04 15:47:14

标签: java variables

我试图编写一个创建随机拼写效果的程序,但是'长度'无法解析变量...

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(" ");
        }
    }
}

2 个答案:

答案 0 :(得分:6)

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

int effect = objects.length;

for (int i = 0; 1 < length; i++) // <-- You are using the wrong variable name here

答案 1 :(得分:0)

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

       int effect = objects.length;

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

OR

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

    for (int i = 0; i < objects.length; i++) {