- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此社区的第一篇文章。开始在我的大学旁听一些 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 )
{
...
}
最后,这只是一个风格评论:
当您发现自己创建了一堆具有相同名称的相同类型的变量,后跟一个基数(point1
,point2
,point3
等),这是一个强烈的暗示,您确实想要一个数组:
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/
我通过 spring ioc 编写了一些 Rest 应用程序。但我无法解决这个问题。这是我的异常(exception): org.springframework.beans.factory.BeanC
我对 TestNG、Spring 框架等完全陌生,我正在尝试使用注释 @Value通过 @Configuration 访问配置文件注释。 我在这里想要实现的目标是让控制台从配置文件中写出“hi”,通过
为此工作了几个小时。我完全被难住了。 这是 CS113 的实验室。 如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。 但是,我们还需要释放所有分配的内存。
我正在尝试使用 ffmpeg 库构建一个小的 C 程序。但是我什至无法使用 avformat_open_input() 打开音频文件设置检查错误代码的函数后,我得到以下输出: Error code:
使用 Spring Initializer 创建一个简单的 Spring boot。我只在可用选项下选择 DevTools。 创建项目后,无需对其进行任何更改,即可正常运行程序。 现在,当我尝试在项目
所以我只是在 Mac OS X 中通过 brew 安装了 qt。但是它无法链接它。当我尝试运行 brew link qt 或 brew link --overwrite qt 我得到以下信息: ton
我在提交和 pull 时遇到了问题:在提交的 IDE 中,我看到: warning not all local changes may be shown due to an error: unable
我跑 man gcc | grep "-L" 我明白了 Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more inf
我有一段代码,旨在接收任何 URL 并将其从网络上撕下来。到目前为止,它运行良好,直到有人给了它这个 URL: http://www.aspensurgical.com/static/images/a
在过去的 5 个小时里,我一直在尝试在我的服务器上设置 WireGuard,但在完成所有设置后,我无法 ping IP 或解析域。 下面是服务器配置 [Interface] Address = 10.
我正在尝试在 GitLab 中 fork 我的一个私有(private)项目,但是当我按下 fork 按钮时,我会收到以下信息: No available namespaces to fork the
我这里遇到了一些问题。我是 node.js 和 Rest API 的新手,但我正在尝试自学。我制作了 REST API,使用 MongoDB 与我的数据库进行通信,我使用 Postman 来测试我的路
下面的代码在控制台中给出以下消息: Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child el
我正在尝试调用一个新端点来显示数据,我意识到在上一组有效的数据中,它在数据周围用一对额外的“[]”括号进行控制台,我认为这就是问题是,而新端点不会以我使用数据的方式产生它! 这是 NgFor 失败的原
我正在尝试将我的 Symfony2 应用程序部署到我的 Azure Web 应用程序,但遇到了一些麻烦。 推送到远程时,我在终端中收到以下消息 remote: Updating branch 'mas
Minikube已启动并正在运行,没有任何错误,但是我无法 curl IP。我在这里遵循:https://docs.traefik.io/user-guide/kubernetes/,似乎没有提到关闭
每当我尝试docker组成任何项目时,都会出现以下错误。 我尝试过有和没有sudo 我在这台机器上只有这个问题。我可以在Mac和Amazon WorkSpace上运行相同的容器。 (myslabs)
我正在尝试 pip install stanza 并收到此消息: ERROR: No matching distribution found for torch>=1.3.0 (from stanza
DNS 解析看起来不错,但我无法 ping 我的服务。可能是什么原因? 来自集群中的另一个 Pod: $ ping backend PING backend.default.svc.cluster.l
我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误: Error creating bean with name 'wel
我是一名优秀的程序员,十分优秀!