这是我用来创建随机名称的类,但是一行不断出错(不是主类)
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 /\
}
答案 0 :(得分: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]);
}
}
答案 1 :(得分:0)
假设你的main方法在另一个类中,把你的代码放在一个方法中,然后调用你的主类中的方法,它应该可以工作。