2 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
循環(huán)應(yīng)該在讀取輸入之后,并且“NOT FOUND”消息應(yīng)該在循環(huán)之后:
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!!!");
}
如果要執(zhí)行多次搜索,則應(yīng)search()多次調(diào)用該方法。

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
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!!!");
}
}
添加回答
舉報(bào)