如何使用switch case语句在C中执行不等式比较? [英] How do I perform inequality comparisons in C using switch case statement?

查看:1317
本文介绍了如何使用switch case语句在C中执行不等式比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个赋值使用switch语句在C中构建程序,该语句读取一个值,如果值大于50,则显示You passed。如果它大于0且小于50,则显示你没有通过。如果它小于0,则显示无效。

我读过关于这个主题的每篇文章,答案都是使用If / Else。我知道如何使用if / else构建它,问题是我必须使用switch case语句。




我尝试过:



 #include< stdio.h> 

int main(){
int x = 0 ;
printf( 输入你的分数:\ n);
scanf( %i,& x);
开关(x)
{
案例(x> = 50 ):
printf( 你通过了);
break ;
case 0
case (x> 0&& x< 50):
printf( 你没有不通过);
break ;
case (x< 0):
printf( 无效);
break ;
}
getch();
return 0 ;
}

解决方案

始终使用正确的工具完成工作。这里正确的工具是if-else。 Period。


与John的解决方案类似,但使用您对开关语句的要求。它将分数分为3个可能的值,并根据这些值执行切换。

  int  x =  0 ; 
printf( 输入你的分数:\ n);
scanf( %i,& x);
switch (x> = 0 ?(x> = 50 1 0 ): - 1 ){
case 0
printf( 你没有传递);
break ;
case 1
printf( 您通过了);
break ;
默认
printf( 无效);
break ;
}


打电话给我有偏见但我更喜欢我的解决方案...... :)

< pre lang =C> #include < < span class =code-leadattribute> stdio.h >

int main(){
unsigned x = 0 ;
printf( 输入你的分数:\ n);
scanf( %u,& x);

/ * 让我们用数学代替* /
int val = x / 50;
switch (val)
{
case 0
printf( 你没有通过。 );
break ;
case 1
case 2
printf( 你过去了。);
break ;
默认
printf( 无效);
break ;
}
getch();
return 0 ;
}


I have this assignment to build a program in C using switch statement, which reads a value and if the value is grater than 50, it displays "You passed". If it's greater than 0 and less than 50, it displays "You didn't pass". And if it's less than 0, it displays "Not valid".
Every article I've read about this topic, the answers were use If/Else. I know how to build it using if/else, the problem is I have to use switch case statement.


What I have tried:

#include <stdio.h>

int main() {
    int x=0;
    printf("Enter your score:\n");
    scanf("%i",&x);
    switch(x)
    {
        case (x>=50):
            printf("You passed");
            break;
        case 0:
        case (x>0 && x<50):
            printf("You didn't pass");
            break;
        case (x<0):
            printf("Not valid");
            break;
    }
    getch();
    return 0;
}

解决方案

Always use the right tools for the job. The right tool here is if-else. Period.


Similar to John's solution, but using your requirement of a switch statement. It breaks the scores into 3 possible values, and performs the switch based on those values.

int x=0;
printf("Enter your score:\n");
scanf("%i",&x);
switch (x >= 0 ? (x >= 50 ? 1 : 0) : -1) {
    case 0:
        printf("You didn't pass");
        break;
    case 1:
        printf("You passed");
        break;
    default:
        printf("Not Valid");
        break;
}


Call me biased but I like my solution better... :)

#include <stdio.h>
 
int main() {
    unsigned x=0;
    printf("Enter your score:\n");
    scanf("%u",&x);

    /*let's use math instead*/
    int val = x/50;
    switch(val)
    {
        case 0:
            printf("You didn't pass.");
            break;
        case 1:
        case 2:
            printf("You passed.");
            break;
        default:
            printf("Not valid");
            break;
    }
    getch();
    return 0;
}


这篇关于如何使用switch case语句在C中执行不等式比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆