C语言switch

来源:百度知道 编辑:UC知道 时间:2024/07/07 21:22:35
y=x-1 -5<x<0 y=x-1 -5<x<=0
y=x x=0 y=x x=1
y=x+1 0<x<8 y=x+1 1<x<8

x取整数。用switch编写,求助帮写出,并说下区别。3Q了
考试前练习,不是我想用switch啊,是题目要求。
#include<stdio.h>
void main()
{
float x,y;
scanf("%f",&x);
switch(x);
{
case -4:
case -3:
case -2:
case -1:y=x-1;break;
case 0:y=x;break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:y=x+1;break;
default:break;
}
printf("y=%f",y);
}

switch里面每行提示case outside of switch in function main
帮忙改下上面的,可能有很多错误。辛苦大家了

#include<stdio.h>
int main()
{
int x,y;
scanf("%d",&x);// 输入X
switch(x)
{
case -4:
case -3:
case -2:
case -1:y=x-1;break;
case 0:y=x;break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:y=x+1;break;
default:break;
}
printf("%d",y); // 输出Y
}
是用C,不是C++
将float换成int

#include<iostream>
using namespace std;

int main()
{
int x,y;
cin >> x; // 输入X
switch(x)
{
case -4:y=x-1;break;
case -3:y=x-1;break;
case -2:y=x-1;break;
case -1:y=x-1;break;
case 0:y=x;break;
case 1:y=x+1;break;
case 2:y=x+1;break;
case 3:y=x+1;break;
case 4:y=x+1;break;
case 5:y=x+1;break;
case 6:y=x+1;break;
case 7:y=x+1;break;
}
cout << y << endl; // 输出Y
}

用IF控