所以我有两个 while 循环。最外面的一个并没有按照我想要的方式循环。
我第一次尝试:
#define true 1
#define false 0
然后将其设置为我在下面所做的。
我的问题是,当我运行该程序时,它会在无效输入后终止。如果输入的内容不正确,我希望它能循环回到开头。
#include <stdio.h>
int main()
{
int user_number;//create a new integer variable
int last_multiple;//create a new integer variable for max in mult table
int stilltrue = "false"; //create integer to use in while
while (stilltrue == "false"){//initiate while loop
printf("Please enter a positive integer you would like the table for and"
" hit enter, then enter the max value of its multiplication "
"table: \n");
//takes two inputs and makes sure they are integers
if(scanf("%d",&user_number) == 1 && scanf("%d",&last_multiple)== 1){
printf("The number you typed was %d and the "
" value was %d \n\n",user_number,last_multiple);
while (last_multiple > 0){
int multiple;//multiple of user_number and last_multiple
multiple = user_number * last_multiple;
printf("%d x %d is %d \n\n", user_number,last_multiple,multiple);
last_multiple = last_multiple - 1;//decrease the last_multiple value by 1
stilltrue = "true";
}
} else {
printf("That was not an integer. \n");
stilltrue = "false"; //This isn't needed here
}break;
}
return 0;
}
那么如果它到达这个位置,我该如何修改才能让它循环回来
else {
printf("That was not an integer. \n");
stilltrue = "false"; //This isn't needed here
}
感谢您的帮助!
最佳答案
#define true 1
#define false 0
这些是指令,#define
指令会导致编译器用标记字符串替换源文件中每次出现的标识符。
只需使用 true
或 false
代替 "true"
和 "false"
。通过将它们用作 "true"
或 "false"
,它们将被视为字符串。因此你的循环不会迭代。
关于c - 如何格式化代码以便触发 while 循环 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486919/