local-host 2018-11-05 06:07 采纳率: 100%
浏览 223

如果语句在 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条回答 默认 最新

  • 游.程 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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度