python小菜 2018-11-05 06:07 采纳率: 0%
浏览 197

如果语句在 c 语言中不能正确工作

I am a new beginner in C This is my code:

#include <stdio.h>

int main(void) {
int choice;
int clientNum;
printf("\nAssume that in the main memory contain 16 frameSize\n");
printf("Each frame has 256 bits\n");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("\nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while(choice !=1 || choice !=2){
        printf("\nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
        printf("\nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
        scanf("%d", &choice);
}
if(choice==1){
  printf("FCFS");
}
if(choice==2){
  printf("Round Robind");
 }
 return 0;
}

I want to compare the value of choice with number 1 and 2. However, If statements did not work correctly. it did not compare choice with any value Is there any error in syntax or logic? Please help me!!! :(

The output:

gcc version 4.6.3


Assume that in the main memory contain 16 frameSize
Each frame has 256 bits
How many clients:  3

Please choose the Scheduling Algorithm 1. FCFS 2.Round Robin:  1

INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin:  2

INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin:  1

INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 

转载于:https://stackoverflow.com/questions/53149153/if-statements-did-not-work-correctly-in-c

  • 写回答

1条回答 默认 最新

  • lrony* 2018-11-05 06:59
    关注

    This should work. And please have a look at your coding style:

    #include <stdio.h>
    
    int main(void) 
    {
        int choice;
        int clientNum;
    
        printf("\nAssume that in the main memory contain 16 frameSize\n");
        printf("Each frame has 256 bits\n");
        printf("How many clients: ");
        scanf("%d", &clientNum);
        printf("\nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
        scanf("%d", &choice);
        while (choice !=1 && choice !=2)
        {
            printf("\nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
            printf("\nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
            scanf("%d", &choice);
        }
        if (choice == 1)
        {
            printf("FCFS");
        }
        if (choice == 2)
        {
            printf("Round Robind");
        }
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?