Console.WriteLine的小用法

我在一开始使用Console.WriteLine的时候,经常采用的是拼接字符串的形式来构建输出。

但是Console.WriteLine具有扩展的方法来对内容进行输出,类似于我们常用的String.Format。

static void Main(string[] args)
        {
            //generic list
            List<int> ListGeneric = new List<int> { 5, 9, 1, 4 };
            //timer for generic list sort
            Stopwatch s = Stopwatch.StartNew();
            ListGeneric.Sort();
            s.Stop();
           // Console.WriteLine($"Generic Sort:{ListGeneric} \n Time taken:{s.Elaspsed.TotalMillisecond}ms");
            Console.WriteLine("Generic Sort: {0}  \n Time taken: {1}ms", ListGeneric,s.Elapsed.TotalMilliseconds);
            Console.ReadLine();
        }

对于Console.Writeline对其会有特殊的格式,比如文本特殊格式,数字特殊格式,日期特殊格式,枚举特殊格式(enumeration)等。

使用{0}是console.writeline()的基本,在这个基础上,我们可以对其具体格式进行限定。其中D说明的DateTime.Now的标准格式,因为电脑系统和设置的不同,该值存在一定的差异。

Console.WriteLine("Today's date:{0:D}", DateTime.Now);

转载于:https://www.cnblogs.com/zhulinmails/p/6711415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值