从 Java 中的数组获取用户定义的数据
Getting user defined data from an array in Java
我正在尝试在 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();
}
}
在外部定义数组
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]));
}
}
我正在尝试在 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();
}
}
在外部定义数组
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]));
}
}