-1

この課題のために、私の教授は、ユーザーに 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;
}
4

3 に答える 3

4

あなたは課題を逆に書いたようです。言いたかったんでしょうね

position_1 = x;

それ以外の

x=position_1;

等々...

于 2013-10-23T05:25:27.527 に答える
0

これはうまくいく可能性が高いです...コンパイラに問題があるため、試していませんが、うまくいくはずです。

#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();

}

私はほぼ 1 年間 C++ でプログラミングしていませんが、これは C++ では機能しないと思います。

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

私が覚えている限りでは、次のようになります。

cout<<position_1 << " " << position_2 << " " << position_3 <<"\n";
于 2013-10-23T07:18:06.013 に答える
0

これはそれを行う必要があります:

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";
于 2013-10-23T07:23:56.410 に答える