- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, const char * argv[])
- {
- char str[1024];
- printf("请输入一个整数:\n");
- scanf("%s", str);
- for (int i = 0; str[i] != '\0'; i++) {
- //非int 类型有 , 能输入的有 double类型, 字符 & 字符串
- if (str[i] < '0' || str[i] > '9') {
- if (str[i] == '-') {
- continue;
- }
- printf("格式错误! 请重新输入: \n");
- scanf("%s", str);
- i = -1;
- continue;
- }
- }
- int result = atoi(str);
- if (result % 2 == 0) {
- printf("%d是偶数\n", result);
- }
- else {
- printf("%d是奇数\n", result);
- }
- return 0;
- }
复制代码 |