如何在 C# 中從使用者一行輸入多個值?


使用 while 迴圈在一行中從使用者輸入多個值。

假設您需要獲取矩陣的元素。使用 Console.ReadLine() 獲取,如下所示 −

Console.Write("
Enter elements - Matrix 1 : "); for (i = 0; i < m; i++) {    for (j = 0; j < n; j++) {       arr1[i, j] = Convert.ToInt16(Console.ReadLine());    } }

以下是一個示例,展示如何從使用者輸入多個值 −

示例

 線上演示

using System;

namespace Demo {
   public class Program {
      public static void Main(string[] args) {
         int[,] arr1 = new int[10, 10];
         int i, j;

         Console.Write("
Enter Matrix elements: ");          for (i = 0; i < 3; i++) {             for (j = 0; j < 3; j++) {                arr1[i, j] = Convert.ToInt16(Console.ReadLine());             }          }       }    } }

輸出

Enter Matrix elements:

更新時間:2020 年 6 月 22 日

2K+ 觀看

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.