我希望能够帮助您进行程序的错误检查。我要求用户输入一个整数,并希望检查用户输入是否为整数,如果不是,请重复使用scanf。
我的代码:
int main(void){
int number1, number2;
int sum;
//asks user for integers to add
printf("Please enter the first integer to add.");
scanf("%d",&number1);
printf("Please enter the second integer to add.");
scanf("%d",&number2);
//adds integers
sum = number1 + number2;
//prints sum
printf("Sum of %d and %d = %d \n",number1, number2, sum);
//checks if sum is divisable by 3
if(sum%3 == 0){
printf("The sum of these two integers is a multiple of 3!\n");
}else {
printf("The sum of these two integers is not a multiple of 3...\n");
}
return 0;
}
for
、while
和do-while
循环。 - haccksscanf
的内容,特别是它返回的内容。 - Some programmer dude