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!!!");
}
}
添加回答
舉報