作业要求:以表格的形式输出5笔购物金额及总金额
步骤:
1.创建一个长度为5的double类型数组,存储购物金额。
2.循环输入五笔购物金额,并累加总金额。
3.利用循环输出五笔购物金额,最后输出总金额。
作业代码:
try
{
double[] money = new double[5];
double sum = 0;
for (int i = 0; i < money.Length; i++)
{
Console.Write("第{0}个金额为:", i + 1);
money[i] = Convert.Todouble(Console.ReadLine());
sum += money[i];
}
Console.WriteLine("序号\t金额");
for (int i = 0; i < money.Length; i++)
{
Console.WriteLine("{0}\t{1}", i + 1, money[i]);
}
Console.WriteLine("总金额为:{0}", sum);
}
catch
{
Console.WriteLine("你输入的格式有误");
}
Console.ReadKey();
作业效果:
image.png
网友评论