C语言 C 显示输入的最高和最低数字的程序。编程初学者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21844554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:46:38  来源:igfitidea点击:

C Program to display the highest and lowest entered numbers. Programming Beginner

c

提问by aawhite

I'm taking a C programming class this semester and I have absolutely no programming experience, this is making it extremely difficult for me to do the assignments or even study.

这学期我正在上 C 编程课,我完全没有编程经验,这使得我做作业甚至学习都非常困难。

the assignment is to write a program that will allow the user to input any amount of values and display the largest and smallest values that were inputted.

任务是编写一个程序,允许用户输入任意数量的值并显示输入的最大值和最小值。

it has to accept positive and negative number and entering 0 will terminate the program.

它必须接受正负数,输入 0 将终止程序。

if the first number entered is 0 then a message stating this must be displayed.

如果输入的第一个数字是 0,则必须显示一条消息说明这一点。

this may be quite laughable to some of you, but here is what I have.

这对你们中的一些人来说可能很可笑,但这是我所拥有的。

#include <stdio.h>

int main(void)

{ 

float max=0, a;
float min=0, b;

printf("Entering 0 will terminate the sequence of input values.\n");

do{ printf("Enter Number:");

    if (scanf(" %f", &a)==1);{

    if(a<max){

        a=max;}

    if(a>min){

        a=min;}

    }

} while(a!=0);

printf("Your largest number was %.3f. Your smallest number was %.3f.", max, min);

return 0;
}

also, can any of you recommend and reference materials that will help me learn this stuff, thank you.

另外,大家能不能推荐一下可以帮助我学习这些东西的参考资料,谢谢。

回答by wizard23

should be:

应该:

if(a > max) {
    max = a;
}
if(a < min) {
    min = a;
}

回答by herohuyongtao

It should work if you fix the following issues.

如果您解决以下问题,它应该可以工作。

  1. You need to change

    if (scanf(" %f", &a)==1);{
               ^            ^
    

    to

    if (scanf("%f", &a)==1){
    
  2. For

    while(a!=0);
           ^^^
    

    It's a bad practice to compare floatusing !=. Better to use the following instead

    while(fabs(a) > 0.001);
    

    .

    As commented by @JonathanLeffler, actually it will be OK for this case. But in general, you certainly need to be careful about whether it is appropriate to compare two floating point values for equality, especially after a computation

  3. Your logic is wrong, you should update max/mininstead of a. So change

    if(a > max) {
        a = max;
    }
    if(a < min) {
        a = min;
    }
    

    to

    if ( fabs(a) < 0.001 ) // if a~0, stop evaluating
                           //, otherwise, you will always get 0 as the min
        break;
    if(a > max) {
        max = a;
    }
    if(a < min) {
        min = a;
    }
    
  4. You should initialize minto be a very large number (e.g. FLT_MAX) at first to make it able to update based on a < min. And you'd better set float max=FLT_MINif you want to handle negative numbers.

  1. 你需要改变

    if (scanf(" %f", &a)==1);{
               ^            ^
    

    if (scanf("%f", &a)==1){
    
  2. 为了

    while(a!=0);
           ^^^
    

    这是一个不好的做法,比较float使用!=。最好使用以下代替

    while(fabs(a) > 0.001);
    

    .

    正如@JonathanLeffler 所评论的,实际上这种情况是可以的。但总的来说,您当然需要小心比较两个浮点值是否相等,尤其是在计算之后

  3. 你的逻辑是错误的,你应该更新max/min而不是a. 所以改变

    if(a > max) {
        a = max;
    }
    if(a < min) {
        a = min;
    }
    

    if ( fabs(a) < 0.001 ) // if a~0, stop evaluating
                           //, otherwise, you will always get 0 as the min
        break;
    if(a > max) {
        max = a;
    }
    if(a < min) {
        min = a;
    }
    
  4. 您应该首先初始化min为一个非常大的数字(例如FLT_MAX),以使其能够基于 进行更新a < minfloat max=FLT_MIN如果你想处理负数,你最好设置一下。



See it live: http://ideone.com/XQYkeD.

现场观看:http: //ideone.com/XQYkeD

回答by Raging Bull

Try this:

尝试这个:

while(1)
{ 
    printf("Enter Number:");

    if (scanf("%f", &a)==1)
    {
      if(a==0)  //check if the input value is 0 then break the loop
         break;
      else
         {
            if(a>max)
               max=a;

            if(a<min)
               min=a;
         } 
    }
    else
        break;
}

If entered value is greater than max, then max is replaced by that number. And if a is less than min, then min is replaced by that number.

如果输入的值大于 max,则 max 将替换为该数字。如果 a 小于 min,则 min 替换为该数字。