0

这是我为创建随机名称而创建的一个类,但是一行不断出现错误(不是主类)

public class nameGenerator {

String [] namesFirst= {"Micheal","Stewart","Robbinson","Tang"};
String [] namesMiddle= {"Jordan","James","Stanly","Choo" };
String [] namesLast= {"IV","Lee","Persson"};


int a = namesFirst.length;
int b = namesMiddle.length;
int c = namesLast.length;


int x = (int) (Math.random()* a);
int y = (int) (Math.random()* b);   
int z = (int) (Math.random()* c);



System.out.println(namesFirst[x] + namesMiddle[y] + namesLast[z]);
//the error is here /\



}
4

2 回答 2

4

哦使用方法在这里。你不能使用System.out.println()外面的方法。

System.out.println(namesFirst[x] + namesMiddle[y] + namesLast[z]); 
// put inside a method

你必须改变你的代码如下

public class nameGenerator { 
public static void main(String[] args) {
    String [] namesFirst= {"Micheal","Stewart","Robbinson","Tang"};
    String [] namesMiddle= {"Jordan","James","Stanly","Choo" };
    String [] namesLast= {"IV","Lee","Persson"};

    int a = namesFirst.length;
    int b = namesMiddle.length;
    int c = namesLast.length;
    int x = (int) (Math.random()* a);
    int y = (int) (Math.random()* b);
    int z = (int) (Math.random()* c);

    System.out.println(namesFirst[x] + namesMiddle[y] + namesLast[z]);
 }
}
于 2013-09-18T13:00:07.733 回答
0

假设您的主要方法在另一个类中,将您的代码放在一个方法中,然后从您的主类中调用该方法,它应该可以工作。

于 2013-09-18T13:03:24.087 回答