प्राप्त उपयोगकर्ता से इनपुट और बनाने के लिए 2 डी सरणी

0

सवाल

मैं लेने के लिए चाहते हैं उपयोगकर्ता से इनपुट और बनाने के लिए 2 डी सरणी है कि जिस तरह से उपयोगकर्ता इनपुट मूल्यों में दो लाइनों. पहले उपयोगकर्ता परिभाषित मान रिक्त स्थान के द्वारा अलग तो हिट दर्ज करें और देता है एक और मान भी अलग रिक्त स्थान के रूप में नीचे दिखाया गया है पर उदाहरण के लिए:

दे मूल्यों:

2 3 4
5 6 7

चर होनी चाहिए, इस पर अंत:

[[2, 3, 4], [5, 6, 7]]

एक और उदाहरण:

दे मूल्यों:

1 2
3 4

चर होनी चाहिए, इस पर अंत:

[[1, 2], [3, 4]]
c#
2021-11-24 06:06:06
1

सबसे अच्छा जवाब

2

मैं ईमानदारी से पता नहीं क्यों आप यह करना होगा यह जटिल है, लेकिन यहाँ तुम जाओ:

Console.Write("Please insert values separated by white-space: ");
string userInputLine1 = Console.ReadLine();
Console.Write("Please insert values seperated by white-space again: ");
string userInputLine2 = Console.ReadLine();

string[] userInputLine1Splitted = userInputLine1.Split(" ");
string[] userInputLine2Splitted = userInputLine2.Split(" ");

// Either this or catch an out-of-boundary exception when one is larger than the other and fill the space with 0's or something else.
if (userInputLine1Splitted.Length != userInputLine2Splitted.Length)
{
  throw new Exception("Both 1d arrays need to be the same length!");
}

int lengthOfArray = userInputLine1Splitted.Length;

// Since we  always have only 2 rows this can be hardcoded.
int[,] TwoDArrayFromUserInput = new int[2, lengthOfArray]; 

for (int col = 0; col < lengthOfArray; col++)
{
  TwoDArrayFromUserInput[0, col] = Convert.ToInt32(userInputLine1Splitted[col]);
  TwoDArrayFromUserInput[1, col] = Convert.ToInt32(userInputLine2Splitted[col]);
}

// Print to console to prove it worked.
for (int row = 0; row < 2; row++)
{
  for (int col = 0; col < lengthOfArray; col++)
  {
    Console.Write(TwoDArrayFromUserInput[row, col] + " ");
  }

  Console.WriteLine();
}

यदि आप निर्दिष्ट कर सकता है अपने उपयोग के मामले में मैं बहुत यकीन है कि मैं मदद कर सकता है आप एक बेहतर समाधान है ।

2021-11-24 06:43:48

क्यों यू ने कहा कि इसकी जटिल है? कार्य करने के लिए है ले 2D पैसा सरणी से सांत्वना. क्या उस के साथ गलत है?
Arie

शायद "जटिल था" गलत शब्द है । लेकिन उपयोग पर निर्भर करता है-मामला है, वहाँ रहे हैं बेहतर विकल्प का उपयोग की तुलना में 2 -1 डी सरणियों के रूप में एक प्रकार का बफर बनाने के लिए एक 2D सरणी.
Axs

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में