| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 875 人关注过本帖
标题:三只小猪比较体重的问题
只看楼主 加入收藏
后卿
Rank: 4
来 自:网络
等 级:业余侠客
威 望:1
帖 子:302
专家分:295
注 册:2016-10-22
结帖率:81.71%
收藏
已结贴  问题点数:100 回复次数:3 
三只小猪比较体重的问题
输入三只小猪的体重,比较谁最重,打印出来,我的程序如下,不想用&&,因为我觉得我的思路没错,可程序逻辑就是不对,有没有明眼人帮我看看哪里错了
#include<iostream>
#include "string"
using namespace std;
int main()
{
    float A;
    float B;
    float C;
    cout<<"请输入A的体重"<<endl;
    cin>>A;
    cout<<"请输入B的体重"<<endl;
    cin>>B;
    cout<<"请输入C的体重"<<endl;
    cin>>C;
    cout<<"A的体重"<<A<<endl;
    cout<<"B的体重"<<B<<endl;
    cout<<"C的体重"<<C<<endl;
    if (A>B)
    {
        if (B>=C)
            {cout<<"最重的猪是:A1"<<endl;}
        else
        {
            if(A>C)
                {cout<<"最重的猪是:A2"<<endl;}
            else if (A=C)
                {cout<<"最重的猪是:AC3"<<endl;}
            else
                {cout<<"最重的猪是:C4"<<endl;}

        }
    }
    else if(A=B)
    {
        if(A>C)
            {cout<<"最重的猪是:BA5"<<endl;}
        else if(A=C)
            {cout<<"最重的猪是:BAC6"<<endl;}
        else if(A<C)
            {cout<<"最重的猪是:C7"<<endl;}
    }
    else
    {
        if (B=C)
            {cout<<"最重的猪是:BC8"<<endl;}
        else if(B<C)
            {cout<<"最重的猪是:C9"<<endl;}
        else
            {cout<<"最重的猪是:B10"<<endl;}
    }
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: cin cout float 输入 比较 
2022-05-13 00:19
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:100 
先把 if (A=C) 等
都换成 if (A==C) 试试
2022-05-13 06:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <string>
using namespace std;

int main( void )
{
    double a, b, c;
    cout << "请输入A的体重: ";
    cin >> a;
    cout << "请输入B的体重: ";
    cin >> b;
    cout << "请输入C的体重: ";
    cin >> c;
    cout << "A的体重" << a <<endl;
    cout << "B的体重" << b <<endl;
    cout << "C的体重" << c <<endl;

    double heaviest = a;
    string heaviest_list = "A";
    if( b == heaviest )
    {
        heaviest_list.push_back( 'B' );
    }
    else if( b > heaviest )
    {
        heaviest = b;
        heaviest_list = "B";
    }
    if( c == heaviest )
    {
        heaviest_list.push_back( 'C' );
    }
    else if( c > heaviest )
    {
        heaviest = c;
        heaviest_list = "C";
    }
    cout << "最重的猪是: " << heaviest_list << endl;
}
2022-05-13 08:24
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <string>
using namespace std;

int main( void )
{
    double weights[3]; // 以后只需要改这个3就能适应更多数量的猪
    for( size_t i=0; i!=std::size(weights); ++i )
    {
        cout << "请输入" << char('A'+i) << "的体重: ";
        cin >> weights[i];
    }

    for( size_t i=0; i!=std::size(weights); ++i )
    {
        cout << char('A'+i) << "的体重: " << weights[i] << '\n';
    }

    double heaviest = weights[0];
    string heaviest_list;
    for( size_t i=0; i!=std::size(weights); ++i )
    {
        if( weights[i] == heaviest )
            heaviest_list.push_back( char('A'+i) );
        else if( weights[i] > heaviest )
        {
            heaviest = weights[i];
            heaviest_list = char('A'+i);
        }
    }

    cout << "最重的猪是: " << heaviest_list << endl;
}
收到的鲜花
  • 后卿2022-05-13 11:18 送鲜花  1朵   附言:多谢多谢,原来是=和==的问题
2022-05-13 08:33
快速回复:三只小猪比较体重的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015521 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved