I have a litte problem on Console.WriteLine()
. I have my while(true) loop that would check the data if exist and it would allow checking the data 3 times. And inside my loop I have this message:
Console.WriteLine(string.format("Checking data {0}",ctr));
Here's my sample code below:
int ctr = 0;
while(true)
{
ctr += 1;
Console.WriteLine(string.format("Checking data {0}...",ctr))
if(File.Exist(fileName))
break;
if(ctr > 3)
break;
}
Let's assume that data was not found.
My current output show like this:
Checking data 1...
Checking data 2...
Checking dat开发者_运维技巧a 3...
But the correct output that I should want to achieve should looks like this:
Checking data 1...2...3...
I would show only in one line.
Edit:
I forgot: In addition to my problem I want to append "Not Found" and "Found".
Here's the sample Output:
if data found on the First loop output looks like this.
Checking data 1... Found!
if data found on the Second loop output looks like this.
Checking data 1...2...Found!
if data found on the Third loop output looks like this.
Checking data 1...2...3...Found!
AND If data not found
- Checking data 1...2...3... Not Found!
Use Console.Write
instead if you don't want the line break. You also need to move the text out of the loop to avoid repeating it. Something like
Console.WriteLine("Checking data");
int ctr = 0;
bool found = false;
while (ctr++ < 3 && !found) {
Console.Write(" {0}...", ctr);
if (File.Exists(fileName)) found = true;
}
Console.WriteLine(found ? "Found" : "Not found");
Sidenote:
instead of using Console.WriteLine(string.format("Checking data {0}...",ctr));
you could use Console.WriteLine("Checking data {0}...",ctr);
which in my opinion, is easier to read
public static void Main(string[] args)
{
int retries = 0;
bool success = false;
int maxRetries = 3;
string fileName = args[0];
Console.Write("Checking data ");
while(!success && retries++ < maxRetries)
{
Console.Write("{0}...", retries);
success = File.Exists(fileName);
}
Console.WriteLine(" {0}Found!", (success ? "" : "Not ") );
}
You can output "Checking data" before the while loop. So the code will be like this:
Console.Write("Checking data ")
int ctr = 0;
while(true) {
ctr += 1;
Console.Write(string.format("{0}...",ctr))
if (File.Exist(fileName)) break;
if (ctr > 3) break;
}
A better approach (at least I feel so) with reduced condition check:
public static void Main(string[] args)
{
int ctr = 0;
string fileName = args[0];
string result = "Checking data ";
do
{
ctr += 1;
result += ctr.ToString() + "...";
}
while(!File.Exists(fileName) && ctr <= 3);
Console.WriteLine(result);
}
Use Console.Write()
to avoid appending the new line.
To prevent "Checking data" being printed more than once, move it out of the loop.
You have to use
Console.Write()
Try this code:
int ctr = 0;
string result = "Checking data ";
while(true) {
ctr += 1;
result += ctr.ToString() + "...";
if(File.Exist(fileName))
{
result += "Found";
break;
}
if(ctr > 3)
{
result += "Not Found";
break;
}
}
Console.WriteLine(result);
精彩评论