要求:/**
* 作業(yè)2:購物金額結(jié)算
* 步驟:
1.創(chuàng)建一個長度為5的 double類型數(shù)組,存儲購物金額罩引。
2.循環(huán)輸入五筆購物金額蕾总, 并累加總金額筷笨。
3.利用循環(huán)輸出五筆購物金額分冈,最后輸出總金額甜无。
* */
代碼:
namespace 作業(yè)2
{
class Program
{
static void Main(string[] args)
{
/**
* 作業(yè)2:購物金額結(jié)算
* 步驟:
1.創(chuàng)建一個長度為5的 double類型數(shù)組汁针,存儲購物金額答朋。
2.循環(huán)輸入五筆購物金額碗旅, 并累加總金額渡处。
3.利用循環(huán)輸出五筆購物金額,最后輸出總金額祟辟。
* */
try
{
int[] scores = new int[5];
int sum = 0;
Console.WriteLine("請輸入會員本月的消費(fèi)記錄");
for( int i=0;i< scores.Length; i++)
{
Console.WriteLine("請輸入第{0}筆購物金額:",i+1);
scores[i] = Convert.ToInt32(Console.ReadLine());
sum = sum + scores[i];
}
Console.WriteLine(" ");
Console.WriteLine("序號 金額(元)");
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine("{0},{1}",/t , i + 1, scores[i]);
}
Console.WriteLine("總金額 " + sum);
}
catch
{
Console.WriteLine("輸入數(shù)據(jù)有誤医瘫,請重新輸入");
}
Console.ReadKey();
}
}
}
效果圖:
2.jpg