Infinity导致while循环。 [英] Infinity result in while loop.

查看:59
本文介绍了Infinity导致while循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
	int v, t, wc;

	printf("Input temperature (<= 40° F) :...\n");
	scanf_s("%d", &t);

	printf("Input wind speed (>= 6 mph) :...\n");
	scanf_s("%d", &v);

	while (1) /*Calculating the wind chill*/
	{
		if (t <= 40 && v >= 6)
		{
			wc = (int)(0.0817*(3.71*sqrt(v) + 5.81 - 0.25*v)*(t - 91.4) + 91.4);
			printf("The wind chill is %d degrees F.\n", wc);
		}
		else
		{
			printf("Number is out of range. Try again");
		}
	}
	return 0;
}





我的尝试:



您好,我正在制作一个C程序来计算使用while循环的风寒。



然而,当我运行我编写的代码时,它出现风寒是%d度F. \ n,wc无限时间,而我只需要1来显示。



这是我的问题:



1.我应该在哪里移动我的printf(风寒是%d度F.\ n,wc);为了制作它只打印一次?

2.如何将数字四舍五入到单位?

(当我输入t = 22,v = 15时,结果将是 - 1但我希望它是-2,因为它是-1.7507872。)



谢谢!



What I have tried:

Hello, I am making a C program to calculate the wind chill using while loop.

However, when I run the code I wrote, it appears"The wind chill is %d degrees F.\n", wc" infinity times, whereas I just need 1 to display.

Here is my question:

1. Where should I move my "printf("The wind chill is %d degrees F.\n", wc);" in order to make it print only one time?
2. How do I round the number to the units?
(When I input t=22, v=15, the result will be -1 but i want it to be -2 because it is -1.7507872.)

Thanks!

推荐答案

你的while循环语句放错位置。它应该放在第一个printf()之前。



正如PIEBALDConsult指出的那样,你需要有一个中断条件,否则你的循环将永远运行。



Your while loop statement is misplaced. It should be placed before the first printf().

As PIEBALDConsult pointed out, you need to have a break condition, because otherwise your loop will run forever.

int main(void)
{
    int v, t, wc;
    
    while (1) /*Calculating the wind chill*/
    {
        // Not a very good example, but you get the idea
        printf("Enter temp = 100 to exit.\n\n");
  
        printf("Input temperature (<= 40° F) :...\n");
        scanf_s("%d", &t);
        if (t == 100)
            break;
        
        printf("Input wind speed (>= 6 mph) :...\n");
        scanf_s("%d", &v);
    
        if (t <= 40 && v >= 6)
        {
            wc = (int)(0.0817*(3.71*sqrt(v) + 5.81 - 0.25*v)*(t - 91.4) + 91.4);
            printf("The wind chill is %d degrees F.\n", wc);
        }
        else
        {
            printf("Number is out of range. Try again");
        }
    }
    return 0;
}





至于四舍五入,请使用函数 ceil() in math.h

对于向下舍入使用 floor()

只有转换为int才会截断该值,因此1.1变为1.



要进行正确的舍入,您需要检查该值是否较小或大于.5并在ceil()和floor()之间选择。



As for rounding up, use the function ceil() in math.h
For rounding down use floor().
Only casting to an int will truncate the value, hence 1.1 becomes 1.

For proper rounding you need to check if the value is smaller or larger than .5 and chose between ceil() and floor().


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



在调试器上简单运行程序会告诉你重复是因为 loop。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

A simple run of your program on debugger would have show you that the repeating was because of the while loop.


这篇关于Infinity导致while循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆