简体   繁体   English

C#中控制台输出问题

[英]Problems with console output in C#

I have a litte problem on Console.WriteLine() . 我在Console.WriteLine()上有一个小问题。 I have my while(true) loop that would check the data if exist and it would allow checking the data 3 times. 我有我的while(true)循环,它将检查数据是否存在,并允许检查数据3次。 And inside my loop I have this message: 在我的循环中我有这样的信息:

Console.WriteLine(string.format("Checking data {0}",ctr)); 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 data 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". 我忘了:除了我的问题,我想附加“Not Found”和“Found”。

Here's the sample Output: 这是示例输出:

  1. if data found on the First loop output looks like this. 如果在第一个循环输出上找到的数据看起来像这样。

    Checking data 1... Found! 检查数据1 ...找到了!

  2. if data found on the Second loop output looks like this. 如果在第二个循环输出上找到的数据看起来像这样。

    Checking data 1...2...Found! 检查数据1 ... 2 ...找到了!

  3. if data found on the Third loop output looks like this. 如果在第三个循环输出上找到的数据看起来像这样。

    Checking data 1...2...3...Found! 检查数据1 ... 2 ... 3 ...找到了!

AND If data not found AND如果未找到数据

  1. Checking data 1...2...3... Not Found! 检查数据1 ... 2 ... 3 ...未找到!

Use Console.Write instead if you don't want the line break. 如果您不想换行,请使用Console.Write 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)); 而不是使用Console.WriteLine(string.format("Checking data {0}...",ctr));
you could use Console.WriteLine("Checking data {0}...",ctr); 你可以使用Console.WriteLine("Checking data {0}...",ctr); which in my opinion, is easier to read 在我看来,这更容易阅读

You can output "Checking data" before the while loop. 您可以在while循环之前输出“Checking data”。 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);
}
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 ") );
}

Use Console.Write() to avoid appending the new line. 使用Console.Write()来避免追加新行。

To prevent "Checking data" being printed more than once, move it out of the loop. 要防止多次打印“检查数据”,请将其移出循环。

你必须使用

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM