Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีรับค่าหลายค่าจากผู้ใช้ในบรรทัดเดียวใน C#

ใช้ลูป while ร่วมกับ Console.ReadLine() เพื่อรับค่าหลายค่า

ตัวอย่างรับค่าเมทริกซ์:

Console.Write("\nEnter 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("\nEnter Matrix elements: ");
            for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                    arr1[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
        }
    }
}

ผลลัพธ์

Enter Matrix elements: