如何使用 C 语言将单个整数与包含 10 个整数的数组进行比较,以确定该单个整数是否包含在数组中?如果原始问题不清楚,我很抱歉,而滑动等于我想要输出 PORTD=0b10000000 的数组中的任何数字。谢谢!
short a[10]={10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; //Array of accepted passwords.
short array[10];
int i;
for(i=0; i<10; i++){
array[i]=a[i];
}
srand(clock());
while(1){
int swipe=rand() % 20; /* Simulated code read from card swipe, in this
* instance we used a random number between
* 1 and 20.*/
for(i=0; i<10; i++){
if(swipe == array[i]) {
PORTD=0b10000000;
} else {
PORTD=0b00001000;
} //If swiped code evaluates as one of the approved codes then set PORTD RD7 as high.
}
char Master=PORTDbits.RD7;
这似乎已经解决了......感谢您的帮助!
for(i=0; i<10; i++){
if(swipe == array[i]) {
PORTD=0b10000000;
break;
} else {
PORTD=0b00001000;
}
}
最佳答案
您需要根据接受的密码数组中的所有十个值来测试您的滑动值。
例如像下面这样
for(i=0; i<10; i++)
if(swipe == array[i]) {
set a true flag (and maybe exit the for loop)
}
Depending on the flag, set the output
关于在 C 中将单个整数与整数数组进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756799/