如何在 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:
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP