【新手】【java数组】 求帮忙分析一下小弟我这个程序的有关问题所在
【新手求助】【java数组】 求帮忙分析一下我这个程序的问题所在
这是我写的一个程序,就是相当于利用随机数摇号一样,选出一句话的主语,谓语,宾语。然后把这句话打印在 窗口里
然后我编译了一下出来是这个样子的


为什么会出现这个情况,我这样来初始化String类型的数组有问题吗
求帮忙看一下 :P
------解决思路----------------------
这样写试试:
------解决思路----------------------
s1 s2 s3变量不在你的listener的可见范围
类属性变量不能那么定义;
class Test{
int i; //声明变量,默认初始化;
i=1; //程序执行语句;不是变量声明,直接报错.
}
------解决思路----------------------
创建数组的时候把方括号放数组类名后面,“洗澡”后面多了个分号
------解决思路----------------------
类里面只能定义变量,方法,语句块,不能有执行代码。
1楼建议楼主把这些执行代码放在构造方法里面。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestString3 {
public static void main(String[] args) {
new QGame().produce();
}
}
class QGame extends JFrame {
String personSet[] = new String[5];
String place[] = new String[5];
String thing[] = new String[5];
personSet[0] = "姚明";
personSet[1] = "郭敬明";
personSet[2] = "雷军";
personSet[3] = "韩寒";
personSet[4] = "汪峰";
//地点
place[0] = "客厅";
place[1] = "厕所";
place[2] = "卧室";
place[3] = "厨房";
place[4] = "阳台";
//事件
thing[0] = "吃饭";
thing[1] = "睡觉";
thing[2] = "洗澡";;
thing[3] = "打麻将";
thing[4] = "跳广场舞";
TextField tf1, tf2, tf3;//用于生成主谓宾
tf1 = new TextField(10);
tf2 = new TextField(10);
tf3 = new TextField(10);
public void produce() {
Random r = new Random();//用于生成随机数
int rn1 = r.nextInt(5);
int rn2 = r.nextInt(5);
int rn3 = r.nextInt(5);
String s1 = personSet[rn1];
String s2 = place[rn2];
String s3 = thing[rn3];
Label lb = new Label("在");
JButton bt = new JButton("开始恶搞");
bt.addActionListener( new MyMonitor());
setLayout(new FlowLayout());
add(bt);
add(tf1);
add(lb);
add(tf2);
add(tf3);
pack();
setVisible(true);
}
private class MyMonitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf1.setText(s1);
tf2.setText(s2);
tf3.setText(s3);
}
}
}
这是我写的一个程序,就是相当于利用随机数摇号一样,选出一句话的主语,谓语,宾语。然后把这句话打印在 窗口里
然后我编译了一下出来是这个样子的
为什么会出现这个情况,我这样来初始化String类型的数组有问题吗
求帮忙看一下 :P
------解决思路----------------------
这样写试试:
public QGame(){
personSet[0] = "姚明";
personSet[1] = "郭敬明";
personSet[2] = "雷军";
personSet[3] = "韩寒";
personSet[4] = "汪峰";
//地点
place[0] = "客厅";
place[1] = "厕所";
place[2] = "卧室";
place[3] = "厨房";
place[4] = "阳台";
//事件
thing[0] = "吃饭";
thing[1] = "睡觉";
thing[2] = "洗澡";;
thing[3] = "打麻将";
thing[4] = "跳广场舞";
tf1 = new TextField(10);
tf2 = new TextField(10);
tf3 = new TextField(10);
}
------解决思路----------------------
mport java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestString3 {
public static void main(String[] args) {
new QGame().produce();
}
}
class QGame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
String[] personSet = new String[5] ;
String[] place = new String[5];
String[] thing = new String[5];
TextField tf1, tf2, tf3;//用于生成主谓宾
String s1;
String s2;
String s3;
public void produce(){
personSet[0] = "姚明";
personSet[1] = "郭敬明";
personSet[2] = "雷军";
personSet[3] = "韩寒";
personSet[4] = "汪峰";
//地点
place[0] = "客厅";
place[1] = "厕所";
place[2] = "卧室";
place[3] = "厨房";
place[4] = "阳台";
//事件
thing[0] = "吃饭";
thing[1] = "睡觉";
thing[2] = "洗澡";;
thing[3] = "打麻将";
thing[4] = "跳广场舞";
tf1 = new TextField(10);
tf2 = new TextField(10);
tf3 = new TextField(10);
Random r = new Random();//用于生成随机数
int rn1 = r.nextInt(5);
int rn2 = r.nextInt(5);
int rn3 = r.nextInt(5);
s1 = personSet[rn1];
s2 = place[rn2];
s3 = thing[rn3];
Label lb = new Label("在");
JButton bt = new JButton("开始恶搞");
bt.addActionListener( new MyMonitor());
setLayout(new FlowLayout());
add(bt);
add(tf1);
add(lb);
add(tf2);
add(tf3);
pack();
setVisible(true);
}
private class MyMonitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf1.setText(s1);
tf2.setText(s2);
tf3.setText(s3);
}
}
}
s1 s2 s3变量不在你的listener的可见范围
类属性变量不能那么定义;
class Test{
int i; //声明变量,默认初始化;
i=1; //程序执行语句;不是变量声明,直接报错.
}
------解决思路----------------------
创建数组的时候把方括号放数组类名后面,“洗澡”后面多了个分号
------解决思路----------------------
类里面只能定义变量,方法,语句块,不能有执行代码。
1楼建议楼主把这些执行代码放在构造方法里面。