While 循环 - 改变字符串变量
While loop - changing string variable
作为 c 编程的新手(我只有 visual basic 方面的经验),我不完全确定条件语句中带有不断变化的字符串变量的 while 循环应该如何运行。
下面的代码是我制作的一个简单的计算器,它允许用户输入一个运算和两个数字,然后输出相应的结果。我试图在一个 while 循环中编写代码,该循环不断重复该过程,直到用户决定退出它。但是,似乎行 scanf("%c", &quit);不影响 while 循环条件语句。
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = "n";
while (quit = "n"){
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}
提前感谢您的帮助。
将while (quit = "n")
替换为while (quit! = 'y')
那是因为您在 while 循环中为 quit 变量赋值,而不是检查它的值。
对=
使用==
while(quit == "n"){...}
你可以这样做
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = 'n';
//while (quit = "n") //
while (quit!= 'y')
{
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}
作为 c 编程的新手(我只有 visual basic 方面的经验),我不完全确定条件语句中带有不断变化的字符串变量的 while 循环应该如何运行。
下面的代码是我制作的一个简单的计算器,它允许用户输入一个运算和两个数字,然后输出相应的结果。我试图在一个 while 循环中编写代码,该循环不断重复该过程,直到用户决定退出它。但是,似乎行 scanf("%c", &quit);不影响 while 循环条件语句。
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = "n";
while (quit = "n"){
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}
提前感谢您的帮助。
将while (quit = "n")
替换为while (quit! = 'y')
那是因为您在 while 循环中为 quit 变量赋值,而不是检查它的值。
对=
使用==while(quit == "n"){...}
你可以这样做
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = 'n';
//while (quit = "n") //
while (quit!= 'y')
{
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}