#include <stdio.h>
int main()
{
int a, b, c;
/* Input two numbers from user */
printf("Enter two numbers to find maximum: ");
scanf("%d%d", &a, &b);
c = a > b;
switch (c)
{
case 0:
printf("%d is maximum", b);
break;
case 1:
printf("%d is maximum", a);
break;
default:
printf("Invalid Input");
}
return 0;
}
我想在这个C程序中打印默认语句,输入错误的输入,如浮点数或字符常量。每当我输入任何char类型的变量或浮点数时,都会发生这种情况👇
输出示例1:
Enter two numbers to find maximum: 2.5
509 is maximum
输出示例2:
Enter two numbers to find maximum: g
512 is maximum
预期输出应为:
Enter two numbers to find maximum: g
Invalid Input
预期输出应为:
Enter two numbers to find maximum: 22.6
Invalid Input
2条答案
按热度按时间gcxthw6b1#
验证输入和计算表达式应分两个独立的阶段完成:
如果你坚持将状态存储在变量
c
中,你可以这样做:q5iwbnjs2#
c = a > b;
在理论上只能是1或0。你真正想要的可能是这个: