在
Java方面,我是业余爱好者,我正在尝试创建一个小程序,将序列"Black Screen British Guy"随机化为不同的序列。
我已经得到了要打印到控制台的序列,但是我正在尝试将字符串打印到 java 小程序中,我遇到了麻烦,我可以使用一些帮助。谢谢。
package bsbg_gen;
import java.lang.String;
import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.applet.*;
import java.awt.Graphics;
public class main extends Applet {
public void init(){
String[] bsbg;
String[] bsbg2;
String[] bsbg3;
String[] bsbg4;
String [][] name = {{"Black"}, {"Screen"}, {"British"}, {"Guy"}};
String [][] name2 = {{"Black"}, {"Screen"}, {"British"}, {"Guy"}};
String [][] name3 = {{"Black"}, {"Screen"}, {"British"}, {"Guy"}};
String [][] name4 = {{"Black"}, {"Screen"}, {"British"}, {"Guy"}};
Random rand = new Random();
bsbg = name [rand.nextInt(name.length)];
bsbg2 = name2 [rand.nextInt(name2.length)];
bsbg3 = name3 [rand.nextInt(name3.length)];
bsbg4 = name4 [rand.nextInt(name4.length)];
System.out.print(Arrays.toString(bsbg));
System.out.print(Arrays.toString(bsbg2));
System.out.print(Arrays.toString(bsbg3));
System.out.print(Arrays.toString(bsbg4));
}
public void stop(){
}
public void paint(Graphics g){
g.drawString((Arrays.toString(bsbg)), 150, 50);
g.drawString((Arrays.toString(bsbg2)), 150, 50);
g.drawString((Arrays.toString(bsbg3)), 175, 50);
g.drawString((Arrays.toString(bsbg4)), 200, 50);
}
}
您需要将这些声明为类成员。它们目前不在paint
方法的范围内。它们在init
方法中进行了本地化。把它们拿出来。
String[] bsbg;
String[] bsbg2;
String[] bsbg3;
String[] bsbg4;
在外面声明它们
String[] bsbg;
String[] bsbg2;
String[] bsbg3;
String[] bsbg4;
public void init(){