使用循环新手,我制作了一个原始程序,可以从不同的CRN中选择几个班级,然后按所代表的各个学时收费。
现在我可以很容易地在while循环中进行循环,但是我认为我可以在while循环中添加if循环,然后继续循环。我的目标是让每个选择的选项之外的选项都退出循环(在您所选择的选项超出分配的类数量(在这种情况下,最大类数量为3))之外。
虽然似乎不想循环,这是执行do while循环的一种较差的方式,并且有一种更优雅的方式来处理这些循环吗?
do{
printf("Please enter Student ID:\n");
scanf("%i", &studentId);
printf("Enter how may courses-up to 3:\n");
scanf("%f", &amount);
if(amount == 1){
printf("Enter the course number:\n\n");
scanf("%d", &course);
switch(course)
{
case 4587:
credit1 = 4;
break;
case 4599:
credit2 = 3;
break;
case 8997:
credit3 = 1;
break;
case 9696:
credit4 = 3;
break;
default:
printf("Sorry invalid entry!\n\n");
return 0;
}
a = 1;
}
else if(amount == 2){
printf("Enter the 2 course numbers\n");
scanf("%d %d", &course, &course1);
switch(course)
{
case 4587:
credit1 = 4;
break;
case 4599:
credit1 = 3;
break;
case 8997:
credit1 = 1;
break;
case 9696:
credit1 = 3;
break;
default:
printf("Sorry invalid entry!\n\n");
return 0;
}
switch(course1)
{
case 4587:
credit2 = 4;
break;
case 4599:
credit2 = 3;
break;
case 8997:
credit2 = 1;
break;
case 9696:
credit2 = 3;
break;
default:
printf("Sorry invalid entry!\n\n");
return 0;
}
a = 1;
}
else if(amount == 0){
printf("Thank you!\n\n");
a = 1;
}
else if(amount > 3){
printf("Invalid number of courses (up to 3)\n");
a = 0;
}
else{
printf("Sorry, we cant process your request this time-invalid number of courses.\n\n");
printf("\t\t\tTry again. Goodbye!\n");
return 0;
}
}while(a = 0);
答案 0 :(得分:2)
替换行
}while(a = 0);
通过
}while(a == 0);
请执行该操作以允许循环执行。表达式(a = 0)始终被评估为真值,因此循环无法执行