我正在尝试用Java构建一个内置30个代码的程序,如果用户从1到30定义了他想要的代码,那么将向他显示一个特定的代码。到目前为止,我仍然坚持要从数组中获取数据(6位数字)。也许你们中的一些人可以给我一些建议吗?
代码:
package koodibaas;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class koodibaas extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
private TextField tfCount;
private Label lblCount;
private Button btnCount;
public koodibaas() {
setLayout(new FlowLayout());
lblCount = new Label("ENTER CODE?");
add(lblCount);
tfCount = new TextField("0", 10);
add(tfCount);
btnCount = new Button("GIVE ANSWER");
add(btnCount);
btnCount.addActionListener(this);
setTitle("koodibaas");
setSize(400, 150);
setVisible(true);
}
public static void second(String[] args) {
int[] array = new int[30];
array[0] = 111111;
array[1] = 888888;
array[2] = 333333;
array[3] = 555555;
array[4] = 111111;
array[5] = 643208;
array[6] = 643208;
array[7] = 643208;
array[8] = 643208;
array[9] = 643208;
array[10] = 643208;
array[11] = 643208;
array[12] = 643208;
array[13] = 643208;
array[14] = 643208;
array[15] = 643208;
array[16] = 643208;
array[17] = 643208;
array[18] = 643208;
array[19] = 643208;
array[20] = 643208;
array[21] = 643208;
array[22] = 643208;
array[23] = 643208;
array[24] = 643208;
array[25] = 643208;
array[26] = 643208;
array[27] = 643208;
array[28] = 643208;
array[29] = 643208;
}
public static void main(String[] args) {
koodibaas app = new koodibaas();
}
@Override
public void actionPerformed(ActionEvent evt) {
tfCount.setText();
}
}
答案 0 :(得分:0)
在
之外定义你的数组int[] array;
并通过调用第二种方法对其进行初始化。
并从按钮onclick事件访问数组
int number=Integer.parseInt(tfCount.getText());
tfCount.setText(String.valueOf(array[number]));
第二种方法不需要是静态方法,也不需要参数
最后代码应该是
public class koodibaas extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
private int[] array;
private TextField tfCount;
private Label lblCount;
private Button btnCount;
public koodibaas() {
setLayout(new FlowLayout());
lblCount = new Label("ENTER CODE?");
add(lblCount);
tfCount = new TextField("0", 10);
add(tfCount);
btnCount = new Button("GIVE ANSWER");
add(btnCount);
btnCount.addActionListener(this);
setTitle("koodibaas");
setSize(400, 150);
setVisible(true);
second(); // call second to initialize array
}
public void second() {
array = new int[30];
array[0] = 111111;
array[1] = 888888;
array[2] = 333333;
array[3] = 555555;
array[4] = 111111;
array[5] = 643208;
array[6] = 643208;
array[7] = 643208;
array[8] = 643208;
array[9] = 643208;
array[10] = 643208;
array[11] = 643208;
array[12] = 643208;
array[13] = 643208;
array[14] = 643208;
array[15] = 643208;
array[16] = 643208;
array[17] = 643208;
array[18] = 643208;
array[19] = 643208;
array[20] = 643208;
array[21] = 643208;
array[22] = 643208;
array[23] = 643208;
array[24] = 643208;
array[25] = 643208;
array[26] = 643208;
array[27] = 643208;
array[28] = 643208;
array[29] = 643208;
}
public static void main(String[] args) {
koodibaas app = new koodibaas();
}
@Override
public void actionPerformed(ActionEvent evt) {
int number = Integer.parseInt(tfCount.getText());
tfCount.setText(String.valueOf(array[number]));
}
}