Console.WriteLine()格式化输出小记

(1)格式字符串(不考虑大小写,除了e/E)

   C:货币格式  C2:货币格式,精度为两位小数。 eg:$73.23

   D:十进制格式   E:科学计数法  

   System.Console.Write("{0,5:D2}", i);表示宽度为5,精度为2,不足补0。
   D表示是整数,其它的标准数字格式字符串有:
   C 本地货币格式
   E 科学记数法(指数)格式
   F 定点(小数)格式
   G 常规格式
   N 数字格式
   P 百分数格式
   X 十六进制格式
   R 往返过程

   还有一种方式是使用占位符:
   double a = 1.2345;
   System.Console.WriteLine("{0:###.000000}", a);
   结果为1.234500
   “#”号位置上有字符就输出,没有则不输出,0的位置上有字符就输出,没有就填0。
   再来看个例子: 货币格式
   decimal m = 168.24m;
   decimal n = 45.8m;
   System.Console.WriteLine("{0,8:C2} {1,8:C2} {2,8:C2}", m, n,m-n);
输出结果为
¥168.24
¥45.80
¥122.44
前面有一个空格,因为宽度是8,小数点后保留两位小数,不足补0。
它是右对齐的,我们可以换成左对齐:
¥168.24
¥45.80
¥122.44
“¥”符号是自动加上去的,我们这里选择的是货币格式,它会自动选择适当的符号,RMB当然是¥,要修改可以去控制面板里面设置语言和货币。
以上是数字的格式,另外日期和时间格式字符串也是比较常用的。
static void Main(string[] args)
{
DateTime date1 = new DateTime(2010, 5, 22,19,50,28); //2010年5月22日19点50分28秒
Console.WriteLine(date1.ToString("f",CultureInfo.CreateSpecificCulture("zh-CN")));
}

D 长日期模式 2010年5月22日
f 完整日期/时间模式(短时间) 2010年5月22日 19:50
F 完整日期/时间模式(长时间) 2010年5月22日 19:50:28
g 常规日期/时间模式(短时间) 2010/5/22 19:50
G 常规日期/时间模式(长时间) 2010/5/22 19:50:28
M或m 月日模式 5月22日
t 短时间模式 19:50
T 长时间模式 19:50:28
u 通用的可排序日期/时间模式 2010-05-22 19:50:28Z
U 通用完整日期/时间模式 2010年5月22日 11:50:28
Y或y 年月模式 2010年5月
还可以自定义格式,
{
DateTime date1 = new DateTime(2010, 5, 22,19,50,28);
Console.WriteLine(date1.ToString("yyyy年MM月dd日 tt hh:mm:ss.FF",CultureInfo.CreateSpecificCulture("zh-CN")));
}

输出为2010年05月22日 下午 07:50:28
(2)输出写法

{索引,宽度:格式}

宽度:正值右对齐,负值左对齐

通常为:{索引},{索引:格式},{索引,宽度,格式}

(3)以下代码已编译通过:

using System;
namespace NS
{
    class CA
    {
        public static void Main()
        {
            decimal i = 940.23m;
            decimal j = 73.70m;
            Console.WriteLine("{0,9:C2}\n+{1,8:C2}\n----------\n{2,9:C2}",i,j,i+j);
        }
    }
}

结果:Console.WriteLine()格式化输出小记

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Collections.Generic; public class PlaylistApp { public static void Main(string[] args) { LinkedList<string> playlist = new LinkedList<string>(); Stack<string> history = new Stack<string>(); Scanner scanner = new Scanner(System.in); while (true) { Console.WriteLine("Choose an option:"); Console.WriteLine("1. Add a song to your playlist"); Console.WriteLine("2. Play the next song in your playlist"); Console.WriteLine("3. Skip the next song"); Console.WriteLine("4. Rewind one song"); Console.WriteLine("5. Exit"); string input = scanner.nextLine(); if (input.Equals("1")) { Console.WriteLine("Enter Song Name:"); string song = scanner.nextLine(); playlist.AddLast(song); Console.WriteLine(""" + song + "" added to your playlist."); } else if (input.Equals("2")) { if (playlist.Count > 0) { string song = playlist.RemoveFirst(); history.Push(song); Console.WriteLine("Now playing "" + song + """); if (playlist.Count > 0) { Console.WriteLine("Next song: "" + playlist.First.Value + """); } else { Console.WriteLine("Next song: none queued"); } } else { Console.WriteLine("Your playlist is empty."); } } else if (input.Equals("3")) { if (playlist.Count > 0) { string song = playlist.RemoveFirst(); Console.WriteLine("Skipped "" + song + """); if (playlist.Count > 0) { Console.WriteLine("Next song: "" + playlist.First.Value + """); } else { Console.WriteLine("Next song: none queued"); } } else { Console.WriteLine("Your playlist is empty."); } } else if (input.Equals("4")) { if (history.Count > 0) { string song = history.Pop(); //JAVA TO C# CONVERTER TASK: There is no .NET LinkedList equivalent to the 2-parameter Java 'add' method: playlist.add(0, song); Console.WriteLine("Rewound to "" + song + """); if (playlist.Count > 0) { Console.WriteLine("Next song: "" + playlist.First.Value + """); } else { Console.WriteLine("Next song: none queued"); } } else { Console.WriteLine("You haven't played any songs yet."); } } else if (input.Equals("5")) { Console.WriteLine("Goodbye!"); break; } else { Console.WriteLine("Invalid input."); } } scanner.close(); } } 修改代码
06-10
这段代码是C#语言编写的一个控制台应用程序,实现了一个简单的音乐播放器的功能。以下是修改后的代码: ``` using System; using System.Collections.Generic; public class PlaylistApp { public static void Main(string[] args) { LinkedList<string> playlist = new LinkedList<string>(); Stack<string> history = new Stack<string>(); while (true) { Console.WriteLine("Choose an option:"); Console.WriteLine("1. Add a song to your playlist"); Console.WriteLine("2. Play the next song in your playlist"); Console.WriteLine("3. Skip the next song"); Console.WriteLine("4. Rewind one song"); Console.WriteLine("5. Exit"); string input = Console.ReadLine(); if (input.Equals("1")) { Console.WriteLine("Enter Song Name:"); string song = Console.ReadLine(); playlist.AddLast(song); Console.WriteLine($"{song} added to your playlist."); } else if (input.Equals("2")) { if (playlist.Count > 0) { string song = playlist.First.Value; playlist.RemoveFirst(); history.Push(song); Console.WriteLine($"Now playing {song}"); if (playlist.Count > 0) { Console.WriteLine($"Next song: {playlist.First.Value}"); } else { Console.WriteLine("Next song: none queued"); } } else { Console.WriteLine("Your playlist is empty."); } } else if (input.Equals("3")) { if (playlist.Count > 0) { string song = playlist.First.Value; playlist.RemoveFirst(); Console.WriteLine($"Skipped {song}"); if (playlist.Count > 0) { Console.WriteLine($"Next song: {playlist.First.Value}"); } else { Console.WriteLine("Next song: none queued"); } } else { Console.WriteLine("Your playlist is empty."); } } else if (input.Equals("4")) { if (history.Count > 0) { string song = history.Pop(); playlist.AddFirst(song); Console.WriteLine($"Rewound to {song}"); if (playlist.Count > 0) { Console.WriteLine($"Next song: {playlist.First.Value}"); } else { Console.WriteLine("Next song: none queued"); } } else { Console.WriteLine("You haven't played any songs yet."); } } else if (input.Equals("5")) { Console.WriteLine("Goodbye!"); break; } else { Console.WriteLine("Invalid input."); } } } } ``` 主要的修改包括: 1. 移除了 `Scanner` 对象,改用 `Console.ReadLine()` 方法读取用户输入。 2. 修改了字符串的格式化方式,使用了字符串插值(interpolation)的方式。 3. 修改了 `LinkedList` 的 `add` 方法,改用了 `AddFirst` 方法。 4. 修改了变量的命名方式,采用了 `camelCase` 的命名规范。 5. 修改了部分字符串的拼接方式,改用了字符串插值的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值