PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] How do I add an integer to a string using Console.WriteLine?

 July 20, 2022     c#, integer, string     No comments   

Issue

I'm currently writing a program in C# and I'm practicing using integers. I'm trying to make it so the written line comes out with the int at the end. It doesn't give me the outcome I want though, and when I searched on Google it didn't give me the solution I was looking for.

Code:

Random rnd = new Random();
int Pi = rnd.Next(1,5); 

Console.WriteLine("Checking 'Pi' value...");
Console.Beep(37,1000);
Console.WriteLine("Pi value found. Pi = ",Pi);
Console.ReadKey(); 

Outcome:

Checking 'Pi' value...
Pi value found. Pi =

If you know anything I can try, please let me know.


Solution

There are many ways to do this:

// placeholder
Console.WriteLine("Pi value found. Pi = {0}",Pi);

// concatenation
Console.WriteLine("Pi value found. Pi = " + Pi.ToString());

// interpolation
Console.WriteLine($"Pi value found. Pi = {Pi}");

// StringBuilder
var sb = new StringBuilder("Pi value found. Pi = ").Append(Pi);
Console.WriteLine(sb);

// multiple writes
Console.Write("Pi value found. Pi = ");
Console.WriteLine(Pi);


Answered By - Joel Coehoorn
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing