【发布时间】:2014-01-31 04:37:21
【问题描述】:
这是我的任务。
- 如果随机 3 位数字与用户 3 位数字完全匹配 然后是一些奖项。
- 如果用户匹配 3 位数字但不按顺序匹配,则其他一些 奖项。
- 如果用户匹配 2 位数字,则有其他奖品
- 如果用户输入仅匹配 1 位数字,则有其他奖励
这是我想出的:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int r,i,num[0];
srand(time(NULL));
r = rand()%(999-100+1)+100;
printf("Here is the wininng number%d\n",r);
printf("Enter three digit number to win lottery:\n");
scanf("%d",num);
for(i=0;i<3;i++){
if(r==num[0]){
printf("For three exact match you get $100,000\n");
}else if((r/10)==(num[1]/10)){
printf("For two number match you get $50,000\n");
}else if((r%10)==(num[]%10)){
printf("For one number match you get $10,000\n");
}else{
printf("You get nothing!\n");
}}
}
我得到三位数的匹配,有时编译后三位数和两位数匹配。告诉我出了什么问题。提前谢谢你们。
【问题讨论】:
-
我可以在逻辑和代码中看到很多问题。例如 num 被声明为 1 个元素的数组,但您仍然在访问数组的第 2 个元素
标签: c logical-operators