我正在製作一個簡單的程序,它將要求輸入,將其放入一個數組(數字[i])並將其轉換爲三個不同的單位。將其轉換爲不同的單位後,我必須能夠顯示用戶提供的最低和最高號碼。顯示來自用戶輸入的數組的最小值和最大值
我使用的是do while循環,從用戶獲取輸入,以及do while loop
後,我已經把for loop
,將計算從輸入即[I]數組數內的最小和最大數量。但輸入2個輸入後,該程序只顯示了我的最高號碼,最低的號碼顯示我0
。
下面是它的樣子。
do{
clrscr();
cout<<"Enter a number:";
cin>>number[i];
cout<<"\nPlease choose a unit";
cout<<"\n<A> meter";
cout<<"\n<B> centimeter";
cout<<"\n<C> inch";
cout<<"\n<D> feet";
cout<<"\nPlease enter your chosen unit: ";
cin>>unit;
switch(unit)
{
case 'a':
case 'A':
i=0;
m = number[i] * 1;
cm = 1000 * m;
in = m/0.0254;
ft = m/0.3048;
cout<<"\nYou have chosen meter for unit";
cout<<"\nYou have entered "<<number[i]<<" meters";
cout<<"\n"<<m<<" meters is equal to\n"<<cm<<" centimeters";
cout<<"\n"<<in<<" inches";
cout<<"\n"<<ft<<" feet";
cout<<"\n\nDo you want to input another value? y/n:";
cin>>y;
if(y=='y')
{
x=1;
}
else if(y=='n')
{
x=0;
}
break;
case 'b':
case 'B':
cm = number[i] * 1;
m = cm /1000;
in = cm * 0.393701;
ft = cm * 0.0328084;
cout<<"\nYou have chosen centimeter for unit";
cout<<"\nYou have entered "<<number[i]<<" centimeters";
cout<<"\n"<<cm<<" centimeters is equal to\n"<<m<<" meters";
cout<<"\n"<<in<<" inches";
cout<<"\n"<<ft<<" feet";
cout<<"\n\nDo you want to input another value? y/n:";
cin>>y;
if(y=='y')
{
x=1;
}
else if(y=='n')
{
x=0;
break;
}
break;
case 'c':
case 'C':
in = number[i] * 1;
m = in * 0.0254;
cm = in * 2.54;
ft = in * 0.0833333;
cout<<"\nYou have chosen inches for unit";
cout<<"\nYou have entered "<<number[i]<<" inches";
cout<<"\n"<<in<<" inches is equal to\n"<<m<<" meters";
cout<<"\n"<<cm<<" centimeters";
cout<<"\n"<<ft<<" feet";
cout<<"\n\nDo you want to input another value? y/n:";
cin>>y;
if(y=='y')
{
x=1;
}
else if(y=='n')
{
x=0;
}
break;
case 'd':
case 'D':
ft = number[i] * 1;
m = ft*0.3048;
in = ft*12;
cm = ft * 30.48;
cout<<"\nYou have chosen feet for unit";
cout<<"\nYou have entered "<<number[i]<<" feet";
cout<<"\n"<<ft<<" feet is equal to\n"<<m<<" meters";
cout<<"\n"<<cm<<" centimeters";
cout<<"\n"<<in<<" inches";
cout<<"\n\nDo you want to input another value? y/n:";
cin>>y;
if(y=='y')
{
x=1;
}
else if(y=='n')
{
x=0;
}
break;
default:
cout<<"\nYou have entered an invalid choice!";
x=1;
break;
}
}while(x==1);
for(i=0; i<sizeof(number)/sizeof(*number);i++)
{
if(mn>number[i])
{
mn=number[i];
}
else if (mx<number[i])
mx= number[i];
}
cout<<"\nHighest number is : "<<mx;
cout<<"\nLowest number is: "<<mn;
評論是不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/130032/discussion-on-question-by-james-bong-show-min-and-max-values-of-array-那意志)。 – deceze