在 C 控制台窗口中,我无法让这个应用程序检查每个检查是否一致,所以就像在 java 中那样而不是 if "while"然后它会改变,只要条件不匹配它就会一遍又一遍地循环直到你中断出
我如何在 C 语言中做到这一点?
case 1: //add
printf(OPENWINDOW);
printf("\t\tAdd A Reminder (enter the number)\n\n\n\n");
printf("for which day? \n\n");
int tempday;
scanf("%i", &tempday);
if ( tempday >= 32){
printf("sorry we dont go that high on days.. try again!\n");
scanf("%i", &tempday);}
printf("and which month my king? \n\n");
int tempmonth;
scanf("%i", &tempmonth);
if ( tempmonth > 12 ) {
printf("sorry we dont go that high on month... try again!\n");
scanf("%i", &tempmonth);}
printf("what year?\n\n");
int tempyear;
scanf("%i", &tempyear);
while ( tempyear < 2012 || tempyear > 2020 ){
printf("Wow that is an incorrect year and you know it, try again\n");
scanf("%i", tempyear);
printf("What do you want to call this reminder?");
// scanf("%c", char titletemp[]);
}
break;
case 2: // view
printf(OPENWINDOW);
最佳答案
While 在 C 中的工作方式与在 Java 中一样。你用 while 包裹了一段代码,它里面的任何东西都会运行,直到条件为假。
但是,您的scanf
是错误的:
scanf("%i", tempyear);
应该是:
scanf("%i", &tempyear);
^
注意&
,意思是“传递这个变量的地址”。
关于C/C++ 如何运行相同的 switch case 直到它的条件为真并继续而不会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012632/