英文:
Why my codes 'if statement' doesn't work in C?
问题
I wrote a program that you input your qualities and the program decides that if you are suitable for being an astronaut. However, it works when taking the inputs and doesn't continue to the 'if statement' part.
There are no errors and I don't think there is a syntax error either. It takes the inputs and then jumps to the return(0) part. Please help me with this.
COMPLIER:
Enter the minimum weight an astronaut can be>50
Enter the maximum weight an astronaut can be>150
Enter the minimum age an astronaut can be>20
Enter the maximum age an astronaut can be>50
Enter your weight>55
Enter your age>19
Enter the state of smoking (if a smoker press 'S' if not press another letter and press return)>j
Program ended with exit code: 0
英文:
I wrote a program that you input your qualities and the program decides that if you are suitable for being astronaut.However, it works when taking the inputs and doesn't continue to the 'if statement' part.
CODE :
#include <stdio.h>
int
main(void)
{
int opt_min;
int opt_max;
int age_min;
int age_max;
int age;
double weight;
char smoking;
printf("Enter the minimum weight an astronaut can be>");
scanf("%d", &opt_min);
printf("Enter the maximum weight an astronaut can be>");
scanf("%d", &opt_max);
printf("Enter the minimum age an astronaut can be>");
scanf("%d", &age_min);
printf("Enter the maximum age an astronaut can be>");
scanf("%d", &age_max);
printf("Enter your weight>");
scanf("%lf", &weight);
printf("Enter your age>");
scanf("%d", &age);
printf("Enter the state of smoking\n(if a smoker press 'S' if not press another letter and press return)>");
scanf(" %c", &smoking);
if (weight >= opt_min && weight <= opt_max) {
if (age >= age_min && age <= age_max) {
if (smoking != 'S' || smoking != 's') {
printf("You can be an astronaut!");
}
}
}
else {
printf("Sorry! Your qualities are not enough to be an astronout.");
}
return (0);
}
There are no errors and I don't think there is syntax error either. It takes the inputs and then jumps to the return(0) part. Please help me with this.
COMPLIER:
Enter the minimum weight an astronaut can be>50
Enter the maximum weight an astronaut can be>150
Enter the minimum age an astronaut can be>20
Enter the maximum age an astronaut can be>50
Enter your weight>55
Enter your age>19
Enter the state of smoking
(if a smoker press 'S' if not press another letter and press return)>j
Program ended with exit code: 0
答案1
得分: 2
如果 weight
在范围内,但 age
或 smoking
不在范围内,将不会有输出。
将以下内容更改为:
if ((weight >= opt_min) && (weight <= opt_max) &&
(age >= age_min) && (age <= age_max) &&
(smoking != 'S') && (smoking != 's')) {
printf("You can be an astronaut!");
}
else {
printf("Sorry! Your qualities are not enough to be an astronaut.");
}
英文:
If the weight
is within range but age
or smoking
is not, you will get no output.
Change:
if (weight >= opt_min && weight <= opt_max) {
if (age >= age_min && age <= age_max) {
if (smoking != 'S' || smoking != 's') {
printf("You can be an astronaut!");
}
}
}
else {
printf("Sorry! Your qualities are not enough to be an astronout.");
}
Into:
if ((weight >= opt_min) && (weight <= opt_max) &&
(age >= age_min) && (age <= age_max) &&
(smoking != 'S') && (smoking != 's')) {
printf("You can be an astronaut!");
}
else {
printf("Sorry! Your qualities are not enough to be an astronout.");
}
答案2
得分: 1
您在if语句中正确使用了逻辑AND运算符,除了最后一个。
要更正:
if (smoking != 'S' && smoking != 's') {
对于您的输入:
Enter your age>19
然后19不在接受的年龄范围内。
而且else部分属于最外层的if语句,控制该部分。
所以if语句的else部分会被跳过。
您可以引入一个额外的变量,用于存储条件的结果,例如:
int valid = (weight >= opt_min && weight <= opt_max) &&
(age >= age_min && age <= age_max) &&
(smoking != 'S' && smoking != 's');
if (valid)
{
puts("You can be an astronaut!");
}
else
{
puts("Sorry! Your qualities are not enough to be an astronaut.");
}
英文:
You are correctly using the logical AND operator in the if statements except the last one
if (smoking != 'S' || smoking != 's') {
You have to write
if (smoking != 'S' && smoking != 's') {
As for your input
Enter your age>19
then 19 is not in the accepted range of ages.
And the else part belongs to the outer most if statement that gets the control
if (weight >= opt_min && weight <= opt_max) {
//...
}
else {
//...
So the else part of the if statement is skipped.
You could introduce one more variable that would store the result of the conditions as for example
int valid = (weight >= opt_min && weight <= opt_max) &&
(age >= age_min && age <= age_max) &&
(smoking != 'S' && smoking != 's');
if ( valid )
{
puts("You can be an astronaut!");
}
else
{
puts("Sorry! Your qualities are not enough to be an astronout.");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论