你的代码中的 “if statement” 为什么在 C 中不起作用?

huangapple go评论68阅读模式
英文:

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 在范围内,但 agesmoking 不在范围内,将不会有输出。

将以下内容更改为:

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 &gt;= opt_min &amp;&amp; weight &lt;= opt_max) {
	if (age &gt;= age_min &amp;&amp; age &lt;= age_max) {
		if (smoking != &#39;S&#39; || smoking != &#39;s&#39;) {
			printf(&quot;You can be an astronaut!&quot;);
		}
	}
}
else {
	printf(&quot;Sorry! Your qualities are not enough to be an astronout.&quot;);
}

Into:

if ((weight &gt;= opt_min) &amp;&amp; (weight &lt;= opt_max) &amp;&amp;
	(age &gt;= age_min) &amp;&amp; (age &lt;= age_max) &amp;&amp;
	(smoking != &#39;S&#39;) &amp;&amp; (smoking != &#39;s&#39;)) {
		printf(&quot;You can be an astronaut!&quot;);
}
else {
	printf(&quot;Sorry! Your qualities are not enough to be an astronout.&quot;);
}

答案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 != &#39;S&#39; || smoking != &#39;s&#39;) {

You have to write

if (smoking != &#39;S&#39; &amp;&amp; smoking != &#39;s&#39;) {

As for your input

Enter your age&gt;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 &gt;= opt_min &amp;&amp; weight &lt;= 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 &gt;= opt_min &amp;&amp; weight &lt;= opt_max) &amp;&amp;
            (age &gt;= age_min &amp;&amp; age &lt;= age_max) &amp;&amp;
            (smoking != &#39;S&#39; &amp;&amp; smoking != &#39;s&#39;);

if ( valid )
{
    puts(&quot;You can be an astronaut!&quot;);
}
else 
{
    puts(&quot;Sorry! Your qualities are not enough to be an astronout.&quot;);
}

huangapple
  • 本文由 发表于 2023年4月11日 02:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定