如何比较int和数组?

时间:2015-08-26 11:49:53

标签: arrays int

我是一名编程学生,我需要帮助比较int和数组。

idNumber =Integer.parseInt(JOptionPane.showInputDialog("Enter ID number:"));  
    for (i = 0; (i < student.length) && (idNumber == student[i]); i++)         {  
            choiceIsGood = true;

我已宣布: int idNumber; final int [] student = {2064009,2062895,2063427};

我想验证该条目是否已在我的数组中注册。 我将如何比较并继续使用boolean = true;

1 个答案:

答案 0 :(得分:0)

1. idNumber =Integer.parseInt(JOptionPane.showInputDialog("Enter ID number:")); 
2. bool choiceIsGood = false; 
3. for (i = 0; (i < student.length); i++)
4. {
5.    if(idNumber == student[i])  // student[i] is an int, idNumber too !
6.    { // if idNumber == 2064009 or 2062895 or 2063427
7.        choiceIsGood = true; // no need to continue to loop over your array
8.    }
9. }
10.if(choiceIsGood)
11.{
12.  // Do some stuff
13.}

几个解释:

行:1。获取用户输入
第2行。在验证系统之外声明var 第3行。浏览阵列中的每个项目
第5行。检查用户输入是否是数组的当前元素
第7行。将true赋给choiceIsGood
第10行。检查我们是否传递了choiceIsGood = true行。

你永远不会比较两个不同类型的物体;但是与您的输入具有相同类型的数组的内容。