我是 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/