我是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;
}
我想将选择的值与数字1和2进行比较。但是,If语句不能正常工作。它没有将选择与任何价值进行比较 语法或逻辑上有错误吗?
输出:
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:
答案 0 :(得分:0)
while ((choice !=1) && (choice !=2))
{
code.....
}
循环后,您将最终得到2个选择,即1或2,因此:
if (choice == 1)
{
printf("FCFS");
}
else
{
printf("Round Robind");
答案 1 :(得分:-1)
这应该有效:
#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) //change || into &&
{
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;
}