Console.Write的问题(@"")......请帮忙 [英] A problem with Console.Write(@"")......Please help

查看:50
本文介绍了Console.Write的问题(@"")......请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者。我想创建一个基本程序,我们在其中重复添加数字。我很成功但我也希望通过不显示当前答案是xxx这一行来使其变得更简单每次我输入一个数字。我希望文本只保留在一个地方,只有当我们得到一个新的答案时才能改变这个数字。这就是为什么我写了一行Console.Write(@),但它没有似乎工作。请告诉我,如果我做错了,请告诉我正确的代码。





Hi,I'm a beginner in C#.I wanted to create a basic program in which we add numbers repeatedly.I was successful but I also wanted to make it more simple by not displaying the line "The current answer is xxx" every time I enter a number.I wanted the text to stay in just one place and only the number to change when we have a new answer.That's why I wrote the line Console.Write(@"") but it doesn't seem to work.Please tell me if I'm doing something wrong and please tell me the correct code.


namespace Tutorial1
{
    class Program
    {
        static void Main(string[] args)
        {
            int answer, current;
            string currenttranslated;

            answer = 0;
            do
            {
                currenttranslated = Console.ReadLine();
                current = int.Parse(currenttranslated);
                answer = current + answer;
                    Console.Write(@"The current answer is {0}",answer);
                Console.WriteLine();
            }
            while (0 == 0);

        }
    }
}

推荐答案

实际上这可能会非常复杂。通常,当您使用Console.Write(Line)时,您将输出到流程的标准输出流 - 这是自时间开始以来在每个平台上都存在的东西。因此,你想要的通常不是一个好主意。



例如,您可以将应用程序的输入和输出重定向到文件以批量工作没有用户交互的方式。



控制台类确实包含了很多用于在控制台窗口内操作文本的东西。可能最简单的方法是在你的循环中发出一个Console.Clear(),这样窗口每次都会重新启动。



否则,你可以得到参与确定窗口大小,然后在写入之前定位光标,然后你进入消隐东西等世界。



看看课程参考如果您需要更多信息,请使用控制台。
This can actually get quite complicated. Generally when you use Console.Write(Line) you are outputting to the process' standard output stream - something which has been around on every platform since the dawn of time. So as such, what you want isn't generally a good idea.

For instance, you can redirect input and output from you application to files to work in a batch manner without user interaction.

The Console class does contain quite a bit of stuff for manipulation of text within a console window though. Probably the easiest way to do this would be to issue a Console.Clear() in your loop so that the window starts afresh each time.

Otherwise, you can get involved with determining the window size, then positioning the cursor prior to writing but then you get into the world of blanking stuff out etc.

Have a look at the class reference for Console if you need more info.


您好swapnil999



您可以尝试在输入之前清除控制台,如下所示:< br $> b $ b

Hi swapnil999

You can try to clear the console before Input like this:

class Program
 {
     static void Main(string[] args)
     {
         int answer, current;
         string currenttranslated;

         answer = 0;
         do
         {
             Console.Write("Enter new number: ");
             currenttranslated = Console.ReadLine();
             Console.Clear();
             current = int.Parse(currenttranslated);
             answer = current + answer;
             Console.Write(@"The current answer is {0}", answer);
             Console.WriteLine();
         }
         while (true);
     }
 }





还要考虑退出程序的可能性(没有无限循环),也许如果输入无效数字?您可以查看 int.TryParse 并退出循环

快乐编码



Johannes



Also think about a possibillity to exit the program (no endless loop), maybe if a non valid number is entered? You could check with int.TryParse and exit the loop
happy coding

Johannes


这不是那么简单,我很害怕!

控制台非常基本 - 它模拟了一种叫做电传打字机的旧设备,其中一纸卷打印出来,所以你不能回到前一行并删除旧信息并写新信息!



幸运的是,你可以用控制台 - 但不能通过写入。

您需要使用 Console.SetCursorPosition [ ^ ]然后写,然后写。
It's not that simple, I'm afraid!
The console is pretty basic - it emulates a very old device called a teletype where a roll of paper was printed on, so you can't "go back" to the previous line and erase the old info and write new!

Fortunately, you can do it with the Console - but not via Write.
You need to use Console.SetCursorPosition[^] instead, then write.


这篇关于Console.Write的问题(@&quot;&quot;)......请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆