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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.