ได้ข้อมูลจากผู้ใช้และสร้าง 2D อาเรย์

0

คำถาม

ฉันต้องการจะนำเข้าข้อมูลจากผู้ใช้และสร้าง 2D ตารางคู่ลำดับการที่ผู้ใช้ inputs ค่าในสองคนแล้วเราพลาดอะไรไปเนี่ย? คนแรกของผู้ใช้กำหนดค่าโดยแยกแต่ละตัวด้วยช่องว่างแล้วครั้งป้อนและทำให้คนอื่นค่าก็แยกทางช่องว่างที่แสดงทางด้านล่าอยู่ตัวอย่าง:

ให้ค่า:

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 เงินตารางคู่ลำดับจากคอนโซล. Whats ผิดกับมันเหรอ?
Arie

บางที"ซับซ้อน"เป็นคนผิดคำพูด แต่ขึ้นอยู่กับการใช้งาน-คดีมันดีขึ้นมากสำหว่าใช้ 21D arrays เป็นแบบแค่เพื่อที่จะสร้าง 2D อาเรย์.
Axs

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่