【问题标题】:Having trouble sorting numbers with only if else statments仅使用 if else 语句对数字进行排序时遇到问题
【发布时间】:2013-10-23 05:22:35
【问题描述】:

对于这项任务,我的教授要求我创建一个程序,该程序要求用户输入 3 个整数并按从大到小的顺序打印出来。 例如:输入是 10, 4, 6 输出应该是 10, 6, 4 我只被允许使用 if else 语句,这就是我到目前为止所拥有的,但是当我编译它说我的变量 position_1 - position_3 没有初始化,我的输出也有问题。

#include <iostream>
using namespace std;
int main()
{
int x;
int y;
int z;
int position_1;
int position_2;
int position_3;

cout<<"Please enter your first integer value"<<endl;
cin>>x;
cout<<"Please enter your second integer value"<<endl;
cin>>y;
cout<<"Finally enter your third integer value"<<endl;
cin>>z;

if(x>y && x>z)
x=position_1;
else if (x  >y && x < z)
x=position_2;
else if (x <y && x > z)
x=position_2;
else 
x=position_3;

if(y>x && y>z)
y=position_1;
else if (y >x && y < z)
y=position_2;
else if (y <x && y > z)
y=position_2;
else 
y=position_3;

if(z>x && z>y)
z=position_1;
else if (z >x && z < y)
z=position_2;
else if (z <x && z > y)
z=position_2;
else 
z=position_3;

cout<<position_1 + " " + position_2 + " " + position_3.\n;

system("pause");
return 0;
}

【问题讨论】:

    标签: c++ if-statement int conditional


    【解决方案1】:

    你似乎把作业写反了。你可能想说

    position_1 = x;
    

    而不是

    x=position_1;
    

    等等……

    【讨论】:

    • 但是现在我得到一个错误 (31): error C2143: syntax error : missing ';'在'{' 1> name_sort3.cpp 之前
    • @JamesRnepJacobs 你的cout 没有意义:position_3.\n;(从末尾开始的第 4 行)。
    • 我修复了那个部分以使其成为 cout
    • @JamesRnepJacobs 我对此不确定,但仔细查看您的代码表明它包含逻辑错误并且不会针对不同的输入执行预期(即排序)。
    【解决方案2】:

    这更有可能工作......我没有尝试过,因为我的编译器有一些问题,但我认为它应该工作。

    #include <iostream>
    using namespace std;
    int main()
    {
    int x, y, z;
    int position_1, position_2, position_3;
    
    cout<<"Please enter your first integer value"<<endl;
    cin>>x;
    cout<<"Please enter your second integer value"<<endl;
    cin>>y;
    cout<<"Finally enter your third integer value"<<endl;
    cin>>z;
    
    if(x>y&&x>z){
        position_1 = x;
        if(y>z){
            position_2 = y;
            position_3 = z;
        }
        else{
            position_2 = z;
            position_3 = y;
        }
    }
    else if(y>x&&y>z){
        position_1 = y;
        if(x>z){
            position_2 = x;
            position_3 = z;
        }
        else{
            position_2 = z;
            position_3 = x;
        }
    }
    else if(z>x&&z>y){
        position_1 = z;
        if(y>x){
            position_2 = y;
            position_3 = x;
        }
        else{
            position_2 = x;
            position_3 = y;
        }
    }
    
    cout << position_1 << " " << position_2 << " " << position_3;
    
    cin.get();
    cin.get();
    
    }
    

    我已经快一年没有用 c++ 编程了,但我认为这在 c++ 中行不通

    cout<<position_1 + " " + position_2 + " " + position_3.\n;
    

    据我记忆,应该是这样的:

    cout<<position_1 << " " << position_2 << " " << position_3 <<"\n";
    

    【讨论】:

      【解决方案3】:

      应该这样做:

      if(x>=y && x>=z)
      {
        position_1 = x;
        if(y>=z)
        {
          position_2 = y;
          position_3 = z;
        }
        else
        {
          position_3 = y;
          position_2 = z;
        }
      }
      else
      {
        if (x>=y || x>=z)
        {
          position_2 = x;
          if(y>=z)
          {
            position_1 = y;
            position_3 = z;
          }
          else
          {
            position_3 = y;
            position_1 = z;
          }
        }
        else
        { 
          position_3 = x;
          if(y>=z)
          {
            position_1 = y;
            position_2 = z;
          }
          else
          {
            position_2 = y;
            position_1 = z;
          }
        }
      }
      
      cout<<position_1<<" "<<position_2<<" "<<position_3<<"\n";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-03
        • 2011-07-03
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多