gpt4 book ai didi

无法弄清楚如何使用 if 语句返回除一个选项之外的任何内容

转载 作者:行者123 更新时间:2023-11-30 18:48:46 25 4
gpt4 key购买 nike

此社区的第一篇文章。开始在我的大学旁听一些 C 类(class),并且在 If 语句方面遇到了麻烦。在盯着并更改代码的变体几个小时后,我仍然没有弄清楚为什么我不能返回除了我设置的“评分”标准之一之外的值。如果有人愿意告诉我语法错误发生在哪里,甚至可能提示我应该重写哪一部分,我将非常感激。另外,如果我的逻辑需要一些温习,我会喜欢指针。再次感谢。

#include <stdio.h>


/* Main function */

int main()
{
int Answer1;
float Answer2;
float Answer3;
int Answer4;
int Answer5;
float Answer6;

int point1;
point1 = 0;
int point2;
point2 = 0;
int point3;
point3 = 0;
int point4;
point4 = 0;
int point5;
point5 = 0;
int point6;
point6 = 0;

char name;
int sum;
int score;
int multiplier1;
int bonus_score;
int counter;
counter = 1;
int x;
x = 1;
int y;
y = 2;
int z;
z = 3;


/*
****************
****************
this is the end of my declaration system, now begins the actual functions.
***************
****************
*/


printf (" Welcome to your career amplitude test! This simple program will tell you how far you'll go in life. \n");
printf (" Remember to write your answer to at least two decimal places. \n \n ");


printf ("1. What is 5 + 27? \n");
scanf ("%i", &Answer1);

printf("2. what is 2.7 - .85? \n");
scanf ("%f", &Answer2);

printf ("3. what is 2.3 - .1 * 4? \n");
scanf ("%f", &Answer3);

printf ("4. what is 123 * 123?\n");
scanf ("%i", &Answer4);

printf ("5. what is 945/5?\n");
scanf ("%i", &Answer5);

printf (" Bonus Question!!!!! \n");
printf (" what is the square root of 105487.19? You have 10 seconds to enter a number (not really though.) \n");
scanf ("%f", &Answer6);


/*
******************
******************
after those are printed / scanned it will come up with a potential scoring
system using if statements and if else
*****************
*****************
*/


if ( Answer1 == 32)
{
point1 = 1;
}
else ( Answer1 != 32);
{
point1 = 0;
}
if ( Answer2 == 1.85 )
{
point2 = 1;
}
else ( Answer2 != 1.85 );
{
point2 = 0;
}
if ( Answer3 == 1.9 )
{
point3 = 1;
}
else ( Answer3 != 1.9 );
{
point3 = 0;
}
if ( Answer4 == 15129 )
{
point4 = 1;
}
else ( Answer4 != 15129 );
{
point4 = 0;
}
if ( Answer5 == 189 )
{
point5 = 0;
}
else ( Answer5 != 189);
{
point5 = 0;
}

if ( Answer6 != 324.787 )
{
point6 = 0;
}
if ( Answer6 = 324.787 )
{
point6 = 1;
}

/*
******************
******************
Now to actauly grade the assignment compared to the scoring system just established.
******************
******************
*/

while (counter < 100)
{
counter = counter+x+y+z;
printf("Processing at a light speed rate equal to %i \n \n \n", counter);
}

/* the above is a joke and just wanted to implement a loop for pratice */

printf(" This is your raw score without the Bonus. \n");

sum = (point1 + point2 + point3 + point4 + point5);
score = sum;

if ( score = 0 )
{
score = 0;
printf (" Score: 0 \n");
printf (" You missed every question! \n");
}

else if ( score = 1 )
{
score = 1;
printf (" Score: 1 out of 5 \n");
printf ( " You only got one question right! The world needs ditch diggers too. \n");
}

else if ( score = 2 )
{
score = 2;
printf (" Score: 2 out of 5 \n");
printf ( " You missed 3 questions, pratice your soft skills \n");
}

else if ( score = 3 )
{
score = 3;
printf (" Score: 3 out of 5 \n" );
printf (" I forsee a future in the hedge fund industry \n");
}

else if ( score = 4 )
{
score = 4;
printf (" Score: 4 out of 5 \n ");
printf (" you could probably cook books for Enron \n");
}


else if ( score = 5)
{
score = 5;
printf (" Score: 5 out of 5 \n");
printf (" Go out there and break code for the CIA \n");
}

printf ("With the bonus considered, your score is now \n");
if ( point6 = 1 )
{
multiplier1 = 2;
}
else if ( point6 = 0)
{
multiplier1 = 1;
}

if ( multiplier1 = 2)
{
bonus_score = score * 2;

printf (" %i", bonus_score );
}
else if ( multiplier1 = 1)
{
bonus_score = score;
printf (" You got the Bonus wrong. Nothing new to see here. \n");
}


return 0;
}

最佳答案

一些事情:

if ( Answer1 == 32)
{
point1 = 1;
}
else ( Answer1 != 32); <<< problem
{
point1 = 0;
}

该代码被解析为

if ( Answer1 == 32 )
{
point1 = 1;
}
else
Answer1 != 32; // expression is evaluated, result is discarded.

{
point1 = 0;
}

因此,point1 = 0; 被无条件执行(在 if 语句体之外)。我猜你一定正在使用 gcc,因为编译器并没有因为其中有裸 block 而对你大喊大叫。

else 不采用控制表达式;你只需写

else
{
point1 = 0;
}

而且,由于您已经将 point1 初始化为 0,因此根本不需要 else 分支;你只需要

if ( Answer1 == 32 )
{
point1 = 1;
}

您在该 if 语句 block 中重复了相同的错误,因此这是您需要处理的第一件事。

其次,不建议将 == 与浮点类型一起使用。大多数浮点值无法精确存储在给定的位数中,因此存储的是近似值。由 scanf 存储到 Answer2 中的近似值可能与直接赋值存储的近似值不同。进一步加剧问题的是,像 1.85 这样的浮点常量具有 double 类型,它使用与 float 不同的表示形式,因此 ==在这种情况下, 甚至更不可能发挥作用。

除非您的空间确实很紧张(但事实并非如此),请使用double而不是float - 您可以获得更大的范围和精度,对于你正在做的事情来说,它不会变慢。

正确的浮点比较很快就会变得非常难看;这是一种方法:

#include <math.h>
#include <float.h>

int EqualEnough( double a, double b, double max_diff )
{
double diff = fabs( a - b );
double a_abs = fabs( a );
double b_abs = fabs( b );
double larger = a_abs > b_abs ? a_abs : b_abs;

return diff <= larger * max_diff;
}

它并不完美,它不能涵盖所有情况,但就您的目的而言,它应该足够好。您可以在 main 之前定义它,并将其称为

if ( EqualEnough( Answer2, 1.85, DBL_EPSILON ) ) 
{
point2 = 1;
}

参见this page有关浮点比较的更完整讨论。

下一个:

if ( score = 0 )

在此和以下 if 语句中,您使用了 = 赋值运算符而不是 == 相等运算符;您实际上分配值0给score。由于赋值表达式的结果是赋值后左侧的值,因此该表达式的计算结果为 0,即 false,因此不会采用该分支。

else if ( score = 1 )

在本例中,score 设置为 1,表达式的值为 1,因此采用分支。因此,对于这些语句,请确保使用 ==:

if ( score == 0 )
{
...
}
else if ( score == 1 )
{
...
}

最后,这只是一个风格评论:

当您发现自己创建了一堆具有相同名称的相同类型的变量,后跟一个基数(point1point2point3 等),这是一个强烈的暗示,您确实想要一个数组:

int point[6] = {0};  // initializes all elements to 0

if ( Answer1 == 32 )
point[0] = 1;

if ( EqualEnough( Answer2, 1.85, DBL_EPSILON ) )
point[1] = 1;

等等。请记住,C 中的数组从 0 开始索引,而不是 1,因此 point 数组中的元素将是 point[0]point[1] 点[2]、...、点[5]。这也使您可以轻松总结您的观点:

for ( size_t i = 0; i < 6; i++ )
sum += points[i];

您不会对 Answer... 变量执行此操作,因为它们具有不同的类型。

关于无法弄清楚如何使用 if 语句返回除一个选项之外的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44702130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com