这是我目前的代码:
如何将方法调用到其他类?
var img = new Image();
答案 0 :(得分:0)
你想创建WordSelect的对象并从另一个类调用selectHundred方法吗?如果是这样,您可以执行以下操作
//This should be in the MainClass.java file
package test;
public class MainClass {
public static void main(String[] args) {
WordSelect ws = new WordSelect();
String[] w5000 = new String[10];//replace this and the following 10 lines with your w5000
w5000[0] = "a";
w5000[1] = "b";
w5000[2] = "c";
w5000[3] = "d";
w5000[4] = "e";
w5000[5] = "f";
w5000[6] = "g";
w5000[7] = "h";
w5000[8] = "i";
w5000[9] = "j";
String[] w100 = ws.selectHundred(w5000); // I believe you already have w5000, and you will the 100 random words in the w100
for(int i=0;i<w100.length;i++) {
System.out.println(w100[i]);
}
}
}
但您需要将现有程序更改为以下内容,以便该方法可以返回100个字符串的数组
//This should be in the WordSelect.java file
package test;
import java.util.Random;
public class WordSelect {
private Random r;
public String[] selectHundred(String[]w5000) {
r = new Random();
int i = 0;
String []w100 = new String [5];
while (i < w100.length) {
int random = r.nextInt(w5000.length);
w100[i] = w5000[random];
i++;
}
return w100;
}
}