我对编程很陌生。我正在使用cs50 IDE,因为我在YouTube上学习C。当输入一个不是"c"或"r"的字母时,我无法使它重新运行程序。
代码如下:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
//rules or calculator
char c = get_char("Insert the letter c for access to the calculator.nInsert the letter r for access to the rules.n");
//calculator access
if (c == 'c')
{
//get first number
float x = get_float("Enter the first numbern");
//get the operation character
char s = get_char("Enter the math operation charactern");
//get second number
float y = get_float("Enter the second numbern");
//addition
if (s == '+')
{
float z = x + y;
printf("%fn", z);
}
//division
else if (s == '/')
{
float z = x / y;
printf("%fn", z);
}
//multiplication
else if (s == '*')
{
float z = x * y;
printf("%fn", z);
}
//subtraction
else if (s == '-')
{
float z = x - y;
printf("%fn", z);
}
//bug response
else
{
printf("Something went wrong. Are you sure you followed the rules?n");
}
}
//rule acess
else if (c == 'r')
{
printf("Choose 2 numbers and a math operator character. Make sure the numbers don't occupy more than 8 bits. Imprecision in floats might happen. Use these as math operator characters: +; -; /; *; otherwise it won't work.");
}
//bug response 2
else
{
printf("Something went wrong. Are you sure you followed the rules?n");
}
}
如果您想让程序重复
char c = get_char("Insert the letter c for access to the calculator.nInsert the letter r for access to the rules.n");
直到输入有效,然后您可以向程序添加一个无限循环,并在确定输入有效后使用break
语句显式地跳出该循环。
如果你想对
行做同样的处理char s = get_char("Enter the math operation charactern");
则可以为该行添加一个额外的循环来重复,直到输入有效为止。
这是一个解决方案,为两行添加一个循环:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
//loop forever until input is valid
for (;;) //equivalent to while(1)
{
//rules or calculator
char c = get_char("Insert the letter c for access to the calculator.nInsert the letter r for access to the rules.n");
//calculator access
if (c == 'c')
{
//loop forever until input is valid
for (;;)
{
//get first number
float x = get_float("Enter the first numbern");
//get the operation character
char s = get_char("Enter the math operation charactern");
//get second number
float y = get_float("Enter the second numbern");
//addition
if (s == '+')
{
float z = x + y;
printf("%fn", z);
break;
}
//division
else if (s == '/')
{
float z = x / y;
printf("%fn", z);
break;
}
//multiplication
else if (s == '*')
{
float z = x * y;
printf("%fn", z);
break;
}
//subtraction
else if (s == '-')
{
float z = x - y;
printf("%fn", z);
break;
}
//bug response
else
{
printf("Something went wrong. Are you sure you followed the rules?n");
}
}
break;
}
//rule acess
else if (c == 'r')
{
printf("Choose 2 numbers and a math operator character. Make sure the numbers don't occupy more than 8 bits. Imprecision in floats might happen. Use these as math operator characters: +; -; /; *; otherwise it won't work.");
break;
}
//bug response 2
else
{
printf("Something went wrong. Are you sure you followed the rules?n");
}
}
}
在评论部分,您还询问了如何使用goto
语句而不是循环。这也是可能的,但它通常被认为是糟糕的编程实践。
这是一个使用goto
而不是额外循环的程序版本:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
retry_1:
//rules or calculator
char c = get_char("Insert the letter c for access to the calculator.nInsert the letter r for access to the rules.n");
//calculator access
if (c == 'c')
{
retry_2:
//get first number
float x = get_float("Enter the first numbern");
//get the operation character
char s = get_char("Enter the math operation charactern");
//get second number
float y = get_float("Enter the second numbern");
//addition
if (s == '+')
{
float z = x + y;
printf("%fn", z);
}
//division
else if (s == '/')
{
float z = x / y;
printf("%fn", z);
}
//multiplication
else if (s == '*')
{
float z = x * y;
printf("%fn", z);
}
//subtraction
else if (s == '-')
{
float z = x - y;
printf("%fn", z);
}
//bug response
else
{
printf("Something went wrong. Are you sure you followed the rules?n");
goto retry_2;
}
}
//rule acess
else if (c == 'r')
{
printf("Choose 2 numbers and a math operator character. Make sure the numbers don't occupy more than 8 bits. Imprecision in floats might happen. Use these as math operator characters: +; -; /; *; otherwise it won't work.");
}
//bug response 2
else
{
printf("Something went wrong. Are you sure you followed the rules?n");
goto retry_1;
}
}
两个解决方案的一个问题是
这一行float y = get_float("Enter the second numbern");
即使用户之前输入了无效的操作符,也将被执行。如果操作符无效,则立即重新提示用户输入有效的操作符可能更有意义。但是,这意味着必须检查两次操作符,一次在上面引用的行之前,一次在之后:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
//loop forever until input is valid
for (;;) //equivalent to while(1)
{
//rules or calculator
char c = get_char("Insert the letter c for access to the calculator.nInsert the letter r for access to the rules.n");
//calculator access
if (c == 'c')
{
//get first number
float x = get_float("Enter the first numbern");
//get the operation character
char s;
for (;;)
{
s = get_char("Enter the math operatorn");
if (
s == '+' ||
s == '-' ||
s == '*' ||
s == '/'
)
{
//input is valid, so break out of loop
break;
}
printf( "Invalid operator!n" );
}
//get second number
float y = get_float("Enter the second numbern");
//addition
if (s == '+')
{
float z = x + y;
printf("%fn", z);
break;
}
//division
else if (s == '/')
{
float z = x / y;
printf("%fn", z);
break;
}
//multiplication
else if (s == '*')
{
float z = x * y;
printf("%fn", z);
break;
}
//subtraction
else if (s == '-')
{
float z = x - y;
printf("%fn", z);
break;
}
break;
}
//rule acess
else if (c == 'r')
{
printf("Choose 2 numbers and a math operator character. Make sure the numbers don't occupy more than 8 bits. Imprecision in floats might happen. Use these as math operator characters: +; -; /; *; otherwise it won't work.");
break;
}
//bug response 2
else
{
printf("Something went wrong. Are you sure you followed the rules?n");
}
}
}