佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1192|回复: 7

谁可帮我解答这个C++ 的问题??

[复制链接]
发表于 25-7-2008 10:48 PM | 显示全部楼层 |阅读模式


#include <stdio.h>


void main()
{
int Input,even,odd,TotalCountInput,TotalSum,sumeven,sumodd,biggest,smallest;
float average;





sumeven=0;
sumodd=0;
even=0;
odd=0;



printf("lease key in integers and (-888 to stop): ";
scanf("%d",&Input);

biggest=Input;
smallest=Input;


while(Input!=-888)
{
        if(Input%2==0)                                                //input is even number
        {       
       
       
        even++;


sumeven=sumeven+Input;


        printf("lease key in integers and (-888 to stop): ";
        scanf("%d",&Input);

        }

                else           //input is odd number

                {
                odd++;


                sumodd=sumodd+Input;       

                printf("lease key in integers and (-888 to stop): ";
        scanf("%d",&Input);
                }
                {
                if(biggest<Input)
                {
                        biggest=Input;
                }
                else if(smallest>Input)
                {       
               
                        smallest=Input;
                }
                }



                }



TotalSum= sumodd+sumeven;
TotalCountInput= odd+even;
average= (float)TotalSum/TotalCountInput;
printf("This is the count of the even number u have entered:%d\n",even);
printf("This is the count of the odd number u have entered:%d\n",odd);
printf("The average is %f\n",average);
printf("The biggest number is %d\n",biggest);
printf("The smallest number is %d\n",smallest);

}


为什么...当我要DISPLAY SMALLEST 的时候 -888也跟著进去 到

SMALLEST 的 VALUE 的??

我以为 当我按 -888 那个LOOP 已经 没有RUN 了..

可是为什么到最后...-888却被INSERT 了...
回复

使用道具 举报


ADVERTISEMENT

发表于 25-7-2008 11:14 PM | 显示全部楼层
#include <stdio.h>
void main()
{
    int Input,even,odd,TotalCountInput,TotalSum,sumeven,sumodd,biggest,smallest;
    float average;
    sumeven=0;
    sumodd=0;
    even=0;
    odd=0;
   
    biggest=0;
    smallest=0;
    do
    {
            printf("lease key in integers and (-888 to stop): ";
            scanf("%d",&Input);
        if(Input == -888) break;
        if(Input%2==0)  //input is even number
        {              
            even++;
            sumeven=sumeven+Input;
        }        
        else
        {
            odd++;
            sumodd=sumodd+Input;      
        }

        if(biggest < Input)
                biggest=Input;
        else if(smallest > Input)
                smallest=Input;

    }while(Input!=-888)//end of while
    TotalSum= sumodd+sumeven;
    TotalCountInput= odd+even;
    average= (float)TotalSum/TotalCountInput;
    printf("This is the count of the even number u have entered:%d\n",even);
    printf("This is the count of the odd number u have entered:%d\n",odd);
    printf("The average is %f\n",average);
    printf("The biggest number is %d\n",biggest);
    printf("The smallest number is %d\n",smallest);

}
回复

使用道具 举报

 楼主| 发表于 25-7-2008 11:18 PM | 显示全部楼层
原帖由 lawty 于 25-7-2008 11:14 PM 发表
#include
void main()
{
    int Input,even,odd,TotalCountInput,TotalSum,sumeven,sumodd,biggest,smallest;
    float average;
    sumeven=0;
    sumodd=0;
    even=0;
    odd=0;
   
    bigge ...



能了~谢谢你大大
回复

使用道具 举报

发表于 26-7-2008 11:48 AM | 显示全部楼层
你明白吗??
重要的你要明白。。。。
回复

使用道具 举报

 楼主| 发表于 26-7-2008 02:12 PM | 显示全部楼层
[quote]原帖由 lawty 于 26-7-2008 11:48 AM 发表
你明白吗??
重要的你要明白。。。。 [/quote

明白你的写法

不明白为什么我的 SMALLEST NUMBER 永远-888
回复

使用道具 举报

发表于 28-7-2008 08:58 AM | 显示全部楼层
因为你的-888要经过
if(biggest<Input)
                {
                        biggest=Input;
                }
                else if(smallest>Input)
                {      
               
                        smallest=Input;
                }

处理了后。。。。才
while(Input!=-888) 不符合
然后就出来了。。。。。

所以你的smallest就会是-888
回复

使用道具 举报

Follow Us
 楼主| 发表于 2-8-2008 11:40 PM | 显示全部楼层
你是不是说因为刚刚在前面
我已经写了这个Stament

biggest=Input;
smallest=Input;


所以它会跑去CHEck



if(biggest<Input)
                {
                        biggest=Input;
                }
                else if(smallest>Input)
                {      
               
                        smallest=Input;
                }


不然的话我写~
while(Input!=-888)

在前面它应该不会去CHEck le 啊~

谢谢麻烦你很久下
回复

使用道具 举报

发表于 3-8-2008 12:41 PM | 显示全部楼层
永遠是-888的原因:
情況一
Input
-888
biggest=Input; //biggest = -888
smallest=Input; //biggest = -888
Output
Please key in integers and (-888 to stop): -888
This is the count of the even number u have entered:0
This is the count of the odd number u have entered:0
The average is -1.#IND00
The biggest number is -888
The smallest number is -888

情況二
Input
1
biggest=Input; //biggest = 1
smallest=Input; //biggest = 1
-888
if(biggest<Input) // 1 < -888 = false
{  biggest=Input;
} else if(smallest>Input) {   // 1 > -888 = true
smallest=Input; //smallest = -888
}
Output
Please key in integers and (-888 to stop): 1
Please key in integers and (-888 to stop): -888
This is the count of the even number u have entered:0
This is the count of the odd number u have entered:1
The average is 1.000000
The biggest number is 1
The smallest number is -888

情況一
biggest=Input;
smallest=Input;
所以還是-888

情況二
因為輸入-888後沒有break或continue所以還是會執行下去的
biggest=Input;
smallest=Input;
在loop外面只會處理第一次的input

還有這個寫法是不夠完整的
if(biggest < Input)
                biggest=Input;
else if(smallest > Input)
                smallest=Input;
lawty忘了幫你改
這樣才對
if((even + odd) == 1) { // 如果even+odd 等於1的話就等於是第一次
   biggest = Input;
   smallest = Input;
  } else {
     if(biggest < Input)
          biggest=Input;
         if(smallest > Input)
          smallest=Input;
  }
因為第一次輸入value的時候
biggest 跟 smallest 都是 0
拿來跟 0 比較實在不對勁
請看看如果輸入 10
biggest = 0
smallest = 0
0 < 10 = true // biggest = 10
因為放 else if 所以 smallest 還是等於 0
再輸入 9
biggest = 10
smallest = 0
10 < 9 = false // biggest = 10
0 > 9 = false // smallest 依然是0 變成永遠是0了
而且如果是負數的話怎麼辦
所以第一次的時候
直接把輸入的value存進biggest跟smallest就好了

如果你用寫while(Input != -888)
到了最後一個 statement 結束之後
他還是會檢查 condition 的
這就是所謂的 loop
while 跟 do while
差別只是 do while 至少會執行一次
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-12-2025 10:30 AM , Processed in 0.139219 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表