c - if 语句在 C 中无法正常工作

标签 c

我是 C 语言的新手 这是我的代码:

#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;
}

我想将 choice 的值与数字 1 和 2 进行比较。但是,If 语句无法正常工作。它没有将选择与任何值(value)进行比较 语法或逻辑有错误吗?

输出:

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: 

最佳答案

 while ((choice !=1) && (choice !=2))
   {    
        code.....
   }  

循环结束后,您将得到 2 个选择:1 或 2,因此:

 if (choice == 1)
{
    printf("FCFS");
}
else
{
    printf("Round Robind");

关于c - if 语句在 C 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53149153/

相关文章:

c - 在 C 中类型转换更大或更小的结构

c - 如何成对反转链表

c - 如何使用位运算符实现逻辑运算符

c - 段错误问题。尝试在 C 中实现双向链表 FIFO 队列

c - 将 GotoBLAS2 与 C 结合使用

c - 包含动态数组的不透明 C 结构

c - 如何在 C 中不使用指针的情况下通过函数传递 2D 数组

c - 程序的数组迭代

c - 返回多维数组的函数

c - Linux和Windows程序有何不同