How to print Text and value from integer on the same line in the console?

后端 未结 2 862
一向
一向 2021-01-14 14:04

This is what I do, but it didn\'t work:

int money;
Console.Writeline(\"Enter how much money you want\";
money=int.Parse(Console.ReadLine());
Console.Writeli         


        
相关标签:
2条回答
  • 2021-01-14 14:23

    You have several options:

    1. Concatenate the strings:

      Console.Writeline("The Money you have now are" + money);
      
    2. Use the format specifier version of Console.WriteLine:

      Console.Writeline("The Money you have now are: {0}", money);
      
    3. Use Console.Write instead:

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

    Note:

    Your code doesn't actually compile due to some missing parentheses, semi-colons and incorrect casing. I would write your code as this:

    Console.WriteLine("Enter how much money you want");
    int money = int.Parse(Console.ReadLine());
    Console.WriteLine("The Money you have now is {0}", money);
    
    0 讨论(0)
  • 2021-01-14 14:31

    As of C# 6, this can be written as:

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

    See: $ - string interpolation (C# reference)

    0 讨论(0)
提交回复
热议问题