c语言switch case语句例子是什么?

如题所述

int I, k, a, b, c, d, e, f;

printf("请输入利润I:\n");

scanf("%d", &I);

a = 100000 * (1/10);

b = (200000 - 100000) * 75/1000;

c = (400000 - 200000) * 5/100;

d = (600000 - 400000) * 3/100;

e = (1000000 - 600000) * 15/1000;

if(I <= 100000)

printf("奖金总数k=%d\n", I/10);

else if(100000 < I && I <= 200000)

printf("奖金总数k=%d\n", a + (I - 100000) * 75/1000);

else if(200000 < I && I <= 400000)

printf("奖金总数k=%d\n", a + b + (I- 200000) * 5/100);

else if(400000 < I && I <= 600000)

printf("奖金总数k=%d\n", a + b + c + (I- 400000) * 3/100);

else if(600000 < I && I <= 1000000)

printf("奖金总数k=%d\n", a + b + c + d + (I- 600000) * 15/1000);

else

printf("奖金总数k=%d\n", a + b + c + d + e + (I -1000000) * 1/100);

遵守switch语句规则

switch语句非常有用,但在使用时必须谨慎。所写的任何switch语句都必须遵循以下规则:

只能针对基本数据类型中的整型类型使用switch,这些类型包括int、char等。对于其他类型,则必须使用if语句。

switch()的参数类型不能为实型 。

case标签必须是常量表达式(constantExpression),如42或者'4'。

case标签必须是惟一性的表达式;也就是说,不允许两个case具有相同的值。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-05-20

#include <stdio.h>

int main(){

int a;

printf("Input integer number:");

scanf("%d",&a);

if(a==1){

printf("Monday\n");

}else if(a==2){

printf("Tuesday\n");

}else if(a==3){

printf("Wednesday\n");

}else if(a==4){

printf("Thursday\n");

}else if(a==5){

printf("Friday\n");

}else if(a==6){

printf("Saturday\n");

}else if(a==7){

printf("Sunday\n");

}else{

printf("error\n");

}

return 0;

}

switch作为C语言程序语句

Switch用在编程中,如C语言中它经常跟Case一起使用,是一个判断选择代码。其功能就是控制流程流转的。

直线翻译:switch语句,即“切换”语句;case即“情况”。

switch语句的语法如下(switch,case,break和default是关键字):

switch ( 变量表达式 ){    case 常量1 :语句;break;    case 常量2 :语句;break;    case 常量3 :语句;break;    ...    case 常量n:语句;break;         default :语句;break;}

   

本回答被网友采纳
第2个回答  2021-05-20

int I, k, a, b, c, d, e, f;

printf("请输入利润I:\n");

scanf("%d", &I);

a = 100000 * (1/10);

b = (200000 - 100000) * 75/1000;

c = (400000 - 200000) * 5/100;

d = (600000 - 400000) * 3/100;

e = (1000000 - 600000) * 15/1000;

if(I <= 100000)

printf("奖金总数k=%d\n", I/10);

else if(100000 < I && I <= 200000)

printf("奖金总数k=%d\n", a + (I - 100000) * 75/1000);

else if(200000 < I && I <= 400000)

printf("奖金总数k=%d\n", a + b + (I- 200000) * 5/100);

else if(400000 < I && I <= 600000)

printf("奖金总数k=%d\n", a + b + c + (I- 400000) * 3/100);

else if(600000 < I && I <= 1000000)

printf("奖金总数k=%d\n", a + b + c + d + (I- 600000) * 15/1000);

else

printf("奖金总数k=%d\n", a + b + c + d + e + (I -1000000) * 1/100);

遵守switch语句规则:

switch语句非常有用,但在使用时必须谨慎。所写的任何switch语句都必须遵循以下规则:

只能针对基本数据类型中的整型类型使用switch,这些类型包括int、char等。对于其他类型,则必须使用if语句。

switch()的参数类型不能为实型 。

case标签必须是常量表达式(constantExpression),如42或者'4'。

case标签必须是惟一性的表达式;也就是说,不允许两个case具有相同的值。

本回答被网友采纳
相似回答