如何以单行显示输出

时间:2016-07-25 18:08:07

标签: c# arrays console

我的程序正在生成输出,但我期望输出与生成的输出不同。 如果我发送6个输入数字,它应该比较数字并生成答案。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution 
{
    static void Main(String[] args) 
    {
        string[] tokens_a0 = Console.ReadLine().Split(' ');

        int a0 = Convert.ToInt32(tokens_a0[0]);
        int a1 = Convert.ToInt32(tokens_a0[1]);
        int a2 = Convert.ToInt32(tokens_a0[2]);

        string[] tokens_b0 = Console.ReadLine().Split(' ');

        int b0 = Convert.ToInt32(tokens_b0[0]);
        int b1 = Convert.ToInt32(tokens_b0[1]);
        int b2 = Convert.ToInt32(tokens_b0[2]);

        if (a0 > b0 || a0 < b0)
        {
            Console.WriteLine(1);
        }
        if (a1 > b1 || a1 < b1)
        {
            Console.WriteLine(1);
        }
        if (a2 > b2 || a2 < b2)
        {
            Console.WriteLine(1);
        }
    }
}

上面的代码生成以下输出:

  

1

     

1

我需要输出显示如下:

  

1 1

如何更改代码以此方式生成输出?

3 个答案:

答案 0 :(得分:4)

Console.WriteLine完全符合名称所说的内容,它会写下您的信息,然后是新行。

如果您希望输出位于同一行,则应使用Console.Write

if (a0 > b0 || a0 < b0)
{
   Console.Write(1 + " ");
}
if (a1 > b1 || a1 < b1)
{
    Console.Write(1 + " ");
}
if (a2 > b2 || a2 < b2)
{
    Console.Write(1 + " ");
}

答案 1 :(得分:1)

您希望将Console.Write()与空格字符一起使用,而不是使用Console.WriteLine()。

if (a0 > b0 || a0 < b0)
   {
    Console.Write(1 + " ");
}
if (a1 > b1 || a1 < b1)
{
    Console.Write(1 + " ");
}
if (a2 > b2 || a2 < b2)
{
    Console.Write(1 + " ");
}

WriteLine()将在输出文本后插入换行符。

有关Console.Write()的信息,请查看文档here以获取有关Console.WriteLine()和here的信息。

答案 2 :(得分:1)

其他答案建议//pass all retrived info from signup field over to the login scene to facilitate user experience override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if (segue.identifier == "showLoginController") { let svc = segue.destinationViewController as! LoginController; print("pass data over to next viewcontroller") } } ,并且他们都是正确的。我只是想添加另一种方法来产生你正在寻找的结果,如果你觉得它有用,可能会对最终输出有更多的控制。

Console.Write
相关问题