如何比较int和array?
how to compare int and array?
我是一名编程学生,我需要帮助比较 int 和数组。
idNumber =Integer.parseInt(JOptionPane.showInputDialog("Enter ID number:"));
for (i = 0; (i < student.length) && (idNumber == student[i]); i++) {
choiceIsGood = true;
我已声明:
整数编号;
final int[] student = { 2064009, 2062895, 2063427 };
我想验证条目是否已在我的数组中注册。
我将如何比较它并继续 boolean = true;
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
行。
你永远不会比较两个不同类型的对象;但是与您输入的类型相同的数组的内容。
我是一名编程学生,我需要帮助比较 int 和数组。
idNumber =Integer.parseInt(JOptionPane.showInputDialog("Enter ID number:"));
for (i = 0; (i < student.length) && (idNumber == student[i]); i++) {
choiceIsGood = true;
我已声明: 整数编号; final int[] student = { 2064009, 2062895, 2063427 };
我想验证条目是否已在我的数组中注册。 我将如何比较它并继续 boolean = true;
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
行。
你永远不会比较两个不同类型的对象;但是与您输入的类型相同的数组的内容。