C#中控制台應用程式console語句同時接受多個資料輸入

來源:互聯網
上載者:User

一、console.write()與console.writeline()的區別

     前者輸出不換行。

二、console.read()與console.readline()的區別

1’Console.Read() Console.ReadLine()方法都是從鍵盤讀入資訊,唯一不同的就是Console.Read() 方法用於獲得使用者輸入的任何值(可以是任何的字母數字值)的ASCII值.Console.ReadLine()呢?用於將獲得的資料儲存在字串變數之中.

2’ Console.Read() 表示從鍵盤讀取字串,不換行。Console.ReadLine ()表示從鍵盤讀取字串後進行換行。

三、同時接受多個資料輸入

 

      通過系統函數,利用數組解決:具體如例:

例1:建立一個一維整型數組,通過接收使用者輸入的10個數進行初始化,並倒序列印。
用Console.ReadLine()和字串分隔符號實現

static void Main(string[] args)

{

    Console.WriteLine("請輸入十個數,並以空格隔開:");

    string string1 =
Console.ReadLine();

string[] string2 =System.Text.RegularExpressions.Regex.Split(string1,@"[ ]+");

    string[] result =newstring[10];

    for (int i = 0; i < 10; i++)

      {

          result[i]= (string2[10 - i - 1]);

          Console.Write(result[i]+" ");

      }   

}

一、console.write()與console.writeline()的區別

     前者輸出不換行。

二、console.read()與console.readline()的區別

1’Console.Read() Console.ReadLine()方法都是從鍵盤讀入資訊,唯一不同的就是Console.Read() 方法用於獲得使用者輸入的任何值(可以是任何的字母數字值)的ASCII值.Console.ReadLine()呢?用於將獲得的資料儲存在字串變數之中.

2’ Console.Read() 表示從鍵盤讀取字串,不換行。Console.ReadLine ()表示從鍵盤讀取字串後進行換行。

三、同時接受多個資料輸入

 

      通過系統函數,利用數組解決:具體如例:

例1:建立一個一維整型數組,通過接收使用者輸入的10個數進行初始化,並倒序列印。
用Console.ReadLine()和字串分隔符號實現

static void Main(string[] args)

{

    Console.WriteLine("請輸入十個數,並以空格隔開:");

    string string1 =
Console.ReadLine();

string[] string2 =System.Text.RegularExpressions.Regex.Split(string1,@"[ ]+");

    string[] result =newstring[10];

    for (int i = 0; i < 10; i++)

      {

          result[i]= (string2[10 - i - 1]);

          Console.Write(result[i]+" ");

      }   

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.