ลบองค์ประกอบของ Array (c #) [ซ้ำ]

Oct 12 2020

ฉันกำลังเล่นเกมทายคำศัพท์และเดาคำศัพท์พื้นฐาน เพื่อให้แน่ใจว่าคำเดิมจะไม่ปรากฏขึ้นอีก (ตัวอย่างคำที่ใช้) ฉันจำเป็นต้องลบคำที่เลือก ฉันได้ค้นหาวิธีแก้ปัญหาทั้งหมดแล้ว แต่ไม่มีวิธีใดได้ผล ขอความช่วยเหลือใด ๆ !

//word bank
  string[] wordBank = {
  "iphone", "airpods", "laptop","computer","calculator","ram","graphics", "cable","internet","world wide web"
  };

  //select random word
  Random wordRand = new Random();
  int index = wordRand.Next(wordBank.Length);
  string wordSelect = wordBank[index];
            
  //delete word so can't be used again 

คำตอบ

dovid Oct 12 2020 at 15:02
List<string> wordBank = new List<string>  {
  "iphone", "airpods", "laptop","computer","calculator","ram","graphics", "cable","internet","world wide web"
  };

//select random word
Random wordRand = new Random();
int index = wordRand.Next(wordBank.Count);
string wordSelect = wordBank[index];

wordBank.RemoveAt(index);
//or
wordBank.Remove(wordSelect);