作業(yè)要求
定義數(shù)組存儲價格库快,并利用循環(huán)輸入产阱。定義變量min保存當(dāng)前的最低價渠退。將min和數(shù)組中的其余元素依次比較江解。
代碼
...
namespace _1
{
class Program
{
static void Main(string[] args)
{
try
{
double[] jg = new double[4];
Console.WriteLine("請輸入4家店的價格");
for (int i = 0; i < 4; i++)
{
Console.WriteLine("第{0}家店的價格:", i + 1);
jg[i] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("");
}
for (int i = 0; i < jg.Length - 1; i++)
{
for (int j = 0; j < jg.Length-1-i; j++)
{
if (jg[j] > jg[j + 1])
{
double temp = jg[j];
jg[j] = jg[j + 1];
jg[j + 1] = temp;
}
}
}
Console.WriteLine("");
Console.WriteLine("最低價格是:{0}",jg[0]);
}
catch
{
Console.WriteLine("輸入格式錯誤");
}
Console.ReadKey();
}
}
}
...
效果
圖片發(fā)自簡書App