如何在控制台的同一行上从整数打印文本和值?

时间:2015-05-21 13:50:33

标签: c#

这就是我的工作,但它没有工作:

int money;
Console.Writeline("Enter how much money you want";
money=int.Parse(Console.ReadLine());
Console.Writeline("The Money you have now are",money)

2 个答案:

答案 0 :(得分:6)

您有几种选择:

  1. 连接字符串:

    Console.Writeline("The Money you have now are" + money);
    
  2. 使用Console.WriteLine的格式说明符版本:

    Console.Writeline("The Money you have now are: {0}", money);
    
  3. 改为使用Console.Write

    Console.Write("The Money you have now are: ");
    Console.Writeline(money);
    
  4. 注意:

    由于缺少括号,分号和不正确的大小写,您的代码实际上并未编译。我会把你的代码写成:

    Console.WriteLine("Enter how much money you want");
    int money = int.Parse(Console.ReadLine());
    Console.WriteLine("The Money you have now is {0}", money);
    

答案 1 :(得分:0)

从C#6开始,这可以写成:

Console.Writeline($"The Money you have now are: {money}");