我将在我的作业中使用这一小部分代码,但是存在一些问题。当我不使用fflush(stdin)
时,它不会扫描basetype
。但是当我使用fflush(stdin)
时,它会扫描basetype
,但scanf
循环内的while
不会继续从缓冲区中获取字符。例如,当我输入2A
时......请帮忙。
#include <stdio.h>
#include <math.h>
int main() {
int operation, basetype, number1;
char num1;
printf("Please enter the number of the operation you would like to perform:");
printf("\n1. Bitwise OR\n2. Bitwise NOT\n3. Bitwise COMPARE\n4. Exit");
scanf("%d", &operation);
if (operation == 1) {
printf("You chose Bitwise OR operation.");
printf("\nPlease enter the first number:");
scanf(" %c", &num1);
fflush(stdin);
printf("\nPlease specify the base(10/16):");
scanf("%d", &basetype);
if (basetype == 10) {
while (num1 != 10) {
number1 = num1 - '0';
if (number1 >= 1 && number1 <= 9) {
printf("ok");
} else
printf("not ok");
scanf("%c", &num1);
}
}
}
return 0;
}
答案 0 :(得分:1)
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(){
System.out.println("###########################");
return "login";
}
@RequestMapping(value="/signup",method=RequestMethod.POST)
public @ResponseBody String signup(){
System.out.println("value################33");
return "success";
}
具有未定义的行为,请勿使用它来尝试刷新标准输入缓冲区。你可以写一下:
fflush(stdin);
您还应该决定是以{ int c; while ((c = getchar()) != EOF && c != '\n') continue; }
还是数字来读取数字。 num1
用于读取单个字符"%c"
以读取十进制格式的数字。
根据您的需要量身定制"%d"
格式:scanf
转换用户指定基数编码的数字,8,10或16,具体取决于初始字符,就像C源代码语法一样对于整数文字。
这是一个简化的程序,可以处理使用%i
前缀输入的十六进制输入,例如0x
0x40
:
64
答案 1 :(得分:0)
以下提议的代码:
Bitwise_OR
操作清楚地显示代码什么都不做,尤其不是按位OR操作
醇>
现在建议的代码:
#include <stdio.h> // scanf(), printf(), perror()
#include <stdlib.h> // exit(), EXIT_FAILURE
//#include<math.h>
enum
{
Bitwise_OR = 1,
Bitwise_NOT,
Bitwise_COMPARE,
Exit
};
int main( void )
{
char operation;
int basetype;
int number1;
char num1;
puts("Please enter the number of the operation you would like to perform:");
printf( "%s",
"1. Bitwise OR\n"
"2. Bitwise NOT\n"
"3. Bitwise COMPARE\n"
"4. Exit");
if( 1 != scanf(" %c",&operation) )
{
perror( "scanf for operation failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
switch( operation )
{
case Bitwise_OR: //<-- bitwise OR never performed
printf("You chose Bitwise OR operation.");
printf("\nPlease enter the first number:");
if( 1 != scanf(" %c",&num1) )
{
perror( "scanf for first number failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
//fflush(stdin);
printf("\nPlease specify the base(10/16):");
if( 1 != scanf("%d", &basetype) )
{
perror( "scanf for base failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
switch(basetype)
{
case 10:
while(num1 != 10) //<-- user entering 0x0A is extremely unlikely
{
number1=num1-'0'; //<-- what if num1 not '0'...'9'
if(number1>=1 && number1<=9)
{
printf("ok");
}
else
printf("not ok");
// either retry first digit or get second digit
scanf(" %c",&num1); //<-- leading space to consume 'white space'
}
break;
case 16:
break;
default:
puts( "base must be 10 or 16" );
break;
} // end switch()
break;
case Bitwise_NOT:
break;
case Bitwise_COMPARE:
break;
case Exit:
break;
default:
break;
} // end switch()
return 0;
} // end function: main