10. Switch statement

/*

Another form of multi-branch judgment is the switch statement.

Requirements:
1. The data should be received correctly
2. The hundredth system should be used to

understand Switch:
1. If the results of multiple cases are the same, you can omit
all the previous results and break, leaving only the last result and break
2. Switch is enclosed in parentheses The result of the expression can only be an integer or a single character
. 3. If the results of multiple Cases are the same, you can also use default
to summarize them into one sentence.
4. The case value after case cannot be repeated
*/
#include "stdio.h"
void main()
{
float x;
int n;

n=0;
printf("Please enter a test score:");
xx: n=scanf("%f",&x);
if(n!=1||x<0||x>100)
{
printf("The test score is entered incorrectly, please re-enter:");
fflush(stdin) ;
goto xx;
}

switch((int)x/10)
{
case 10:case 9:printf("%.2f good\n",x);break;
case 8:printf("%.2f good\n" ,x); break;
case 7:printf("%.2f medium\n",x);break;
case 6:printf("%.2f passed\n",x);break;
default:printf("%.2f failed\n ",x);break;
}
printf("switch ends\n");
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812953&siteId=291194637