C#打印账票
时间: 2023-07-12 09:08:26 浏览: 45
以下是一个简单的 C# 打印账票的示例代码:
```csharp
using System;
public class Program
{
public static void Main()
{
// 创建账票
string[] items = { "苹果", "香蕉", "桔子" };
int[] quantities = { 2, 3, 1 };
double[] prices = { 1.2, 0.8, 1.5 };
// 计算总价
double total = 0;
for (int i = 0; i < items.Length; i++)
{
total += quantities[i] * prices[i];
}
// 打印账票
Console.WriteLine("=====================================");
Console.WriteLine("商品\t数量\t单价\t总价");
Console.WriteLine("-------------------------------------");
for (int i = 0; i < items.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2:C}\t{3:C}", items[i], quantities[i], prices[i], quantities[i] * prices[i]);
}
Console.WriteLine("-------------------------------------");
Console.WriteLine("总价:\t\t\t\t{0:C}", total);
Console.WriteLine("=====================================");
}
}
```
这段代码将创建一个账票,计算总价并将其打印到控制台中。你可以根据需要进行修改和扩展,例如更改打印格式、将账票保存到文件中等。