C#Console.WriteLine不在一行中打印

时间:2018-10-04 13:08:12

标签: c# console

现在,如果运行此命令,它不会在一行中输出每个Console.WriteLine,为什么? 我知道console.writeline完成后会转到下一行,但问题是在打印异常var时它会在打印时跳到下一行,它与其余writeline不在同一行 错误发生在Display()函数内部的6号变量(异常)处,它与写入行的其余部分不在同一行,为什么?

而且在发生错误的地方也没有换行符。

Ans可以是您喜欢的任何数字。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace EquationSolver
{
    class Program
    {
        public static string exception = "No Solution Found yet";
        public static int go = 40;
        public static Decimal x = 0, formul = 0;
        public static Decimal pref = -100000, next = 100000,ans;
        public static Decimal stepval = next / 10;
        public static Decimal prefrem = 1234567890123.1234567890m, nextrem = 1234567890123.1234567890m;
        public static Decimal nextremfirst = 0;
        public static void Answer()
        {
          Console.WriteLine("Enter ans");
          ans = (Convert.ToDecimal(Console.ReadLine()));

        }
        public static void Main(string[] args)
        {
            //Console.WriteLine("Enter ans");
            //Answer(Convert.ToDecimal(Console.ReadLine()));
            Answer();
            //Console.Clear();

            while (true)
            {
                for (var i = 0; i <= go; i++)
                {
                    for (x = pref; x <= next; x += stepval)
                    {
                        formul = x;
                        if (formul < ans)
                            prefrem = x;
                        else if (formul > ans)
                        {
                            if (nextremfirst == 0)
                            {
                                nextrem = x;
                                nextremfirst += 2;
                            }
                        }
                        else if (formul == ans)
                        {
                            AnsFound();
                            break;
                        }
                        else
                        {
                            Error();
                        }
                        Display();
                    }

                    if (formul == ans)
                    {
                        AnsFound();
                        break;
                    }
                    if (prefrem != 1234567890123.1234567890m)
                        pref = prefrem;
                    if (nextrem != 1234567890123.1234567890m)
                        next = nextrem;
                    nextremfirst = 0;
                    stepval /= 10;
                    if (formul != ans)
                        NoAnsyet();

                    //Console.WriteLine();
                }
                Finnish();
            }
        }
        public static void Display()
        {
            //Console.ReadKey();
            //Console.WriteLine("Formul: {0} x: {1} Ans: {2} Status: {3}", //formul, x, ans, exception);

//Here is the error:
            Console.WriteLine("Pref:{0} Next:{1} Step:{2} Formul:{3} x:{4} Ans:{5} Status:{6}",pref,next,stepval,formul,x,ans,exception);
        }
        public static void Finnish()
        {
            if (formul != ans)
                Error();
            exception = "\ncomplete";
            Console.WriteLine(exception);
            pref = -100000;
            next =  100000;
            stepval = next /= 10;
            Console.ReadKey();
            Console.Clear();
            //Console.WriteLine("Enter ans:");
            //Answer(Convert.ToDecimal(Console.ReadLine()));
            Answer();
        }
        public static void AnsFound()
        {
            exception = "\nSolution Found!";
            //Console.WriteLine("x: {0} Ans: {1} Status: {2}", x, ans, exception);
            //Console.WriteLine("Pref:{0} Next: {1} Stepval: {2}     Formul:{3} x:{4} Ans:{5}  Status:{}", pref, next, stepval, formul, x, ans, exception);

        }
        public static void NoAnsyet()
        {
            exception = "\nNo Solution yet...";
            //Console.WriteLine(exception);
        }
        public static void Error()
        {
            exception = "\nNo Solution error!!";
            Console.WriteLine(exception);
        }
    }
}

4 个答案:

答案 0 :(得分:0)

写入新行。您应该尝试

Console.Write()

如果您想在上一次打印后立即打印。

答案 1 :(得分:0)

因为您在不同的位置设置了exception = "\ncomplete";。开头的\n是换行符。

删除\n

exception = "complete";

"\nSolution Found!"之类的其他文本存在相同的问题。


使用字符串插值使字符串格式更易读:

替换

Console.WriteLine("Pref:{0} Next:{1} Step:{2} Formul:{3} x:{4} Ans:{5} Status:{6}",
    pref, next, stepval, formul, x, ans, exception);

作者

Console.WriteLine(
$"Pref:{pref} Next:{next} Step:{stepval} Formul:{formul} x:{x} Ans:{ans} Status:{exception}");

答案 2 :(得分:0)

变量exception的值是为什么如果您放置特殊字符\n意味着控制台将在其他位置打印的原因,这意味着换行,因此您调用的所有方法都具有该特殊字符

解决方案删除特殊字符并使用Console.WriteLineConsole.Write方法

答案 3 :(得分:0)

根据提供的代码,您尚未调用方法,我对代码进行了一些更改        Edited code 并尝试运行代码状态:找不到解决方案 谢谢