使用字符串的 Console.writeline [英] Console.writeline using strings

查看:29
本文介绍了使用字符串的 Console.writeline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题:如何在 VS 中使用 C# 使用 Console.Writeline() 在 CMD 中显示字符串?我知道你使用 + 来表示整数和浮点数.但是你对字符串使用什么?这就是我所拥有的:

A simple question: How do you display strings in the CMD using Console.Writeline() using C# in VS? I Know you use + for ints and floats. But what do you use for strings? This is what i have:

    private string productName;

    public void GetItemData()
    {
        ShowReciept();
    }

    private void ReadItem()
    {    
        Console.WriteLine("Enter the product's name: "); 
        productName = Console.ReadLine();
    }

    private void ShowReciept()
    {
        Console.WriteLine("**** Name of product:", productName);
    }

在 void ShowReciept() 中,它会写出 Console.WriteLine 命令中的所有内容,但产品名称除外.所以它只是空白,产品名称应该是.

In void ShowReciept() it writes out everything in the Console.WriteLine command, exept the product Name. So its just blank were the product name should have been.

推荐答案

可以使用字符串连接:

Console.WriteLine("**** Name of product:" + productName);

或者你可以使用这个:

Console.WriteLine("**** Name of product:{0}", productName);

此外,如果您使用 C# 6 编程,则可以使用字符串插值:

Furthermore, if you program in C# 6, you can use string interpolation:

Console.WriteLine($"**** Name of product:{productName}");

这篇关于使用字符串的 Console.writeline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆