2 回答
TA贡献1816条经验 获得超4个赞
循环应该在读取输入之后,并且“NOT FOUND”消息应该在循环之后:
public static void search(){
search = JOptionPane.showInputDialog(null,"Enter a student ID");
for(int i = 0;i < studentID.length;i++){
if(studentID[i] == Integer.parseInt(search)){
JOptionPane.showMessageDialog(null, studentID[i]);
return;
}
}
OptionPane.showMessageDialog(null,"NOT FOUND!!!");
}
如果要执行多次搜索,则应search()多次调用该方法。
TA贡献1841条经验 获得超3个赞
import javax.swing.*;
public class Stackoverflow1 {
static int[] studentID = {212,214,215,219};
public static void main(String[] args) {
search();
System.exit(0);
}
public static void search(){
String enter_a_student_id = JOptionPane.showInputDialog(null, "Enter a student ID");
for(int i = 0;i < studentID.length;i++){
if(studentID[i] == Integer.parseInt(enter_a_student_id)){
JOptionPane.showMessageDialog(null, studentID[i]);
System.exit(0);
}
}
JOptionPane.showMessageDialog(null,"NOT FOUND!!!");
}
}
添加回答
举报