按索引从数组中删除字符串

时间:2018-02-08 19:45:07

标签: c# arrays string

我正在尝试使用c#构建一个简单的国家资本测验计划。目前,我正在向用户提供来自国家/地区列表的随机国家/地区,并要求输入有关资本的内容。然后有一个条件,检查正确的资本是否等于输入。完成后,我需要能够从每个数组中删除所请求的国家和资本,以便循环不重复它们。这是我到目前为止的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace program {
  class MainClass {
    public static void Main (string[] args) {
      string[] countries = new string[] {"Germany", "Poland", "France", 
                                        "United States", "Russia", "Japan"};
      string[] capitals = new string[] {"berlin", "warsaw", "paris", 
                                       "washington dc", "moscow", "tokyo"};

      int score = 0;
      int length = countries.Length;

      string current_country = countries[random_number];
      string current_capital = capitals[random_number];

      for (int i = 0; i <= length;) {
        Random rnd = new Random();
        int random_number = rnd.Next(0, length);
        Console.WriteLine("What is the capital of {0}?", current_country);
        string capital_input = Console.ReadLine();

        if (capital_input == current_capital) {
          Console.WriteLine("Correct!");
          countries.Remove[random_number];
          capitals.Remove[random_number];
          score += 1;
          continue;
        } else {
          Console.WriteLine("Incorrect!");
          countries.Remove[random_number];
          capitals.Remove[random_number];
          continue;
      }

      if (length == 0) {
        break;
      }
    }

    int score_percent = score * 20;
    Console.WriteLine("Congratulations! You scored {0}% of questions 
                      correct.", score_percent);
  }
}

}

程序无法使用这些错误进行编译:

exit status 1
main.cs(27,34): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
main.cs(28,28): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
main.cs(33,29): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
main.cs(34,28): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
Compilation failed: 4 error(s), 0 warnings

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

考虑使用以下内容:

List<string> remainingCountries = new List<string>(){ "US", "Germany", "Russia"};
void RemoveIncorrectGuess(string countryToRemove)
{
   remainingCountries.Remove(countryToRemove);
}

答案 1 :(得分:0)

您的代码中存在许多问题。如果您想要工作代码,请使用:

 public static void Main(string[] args) {
    List<string> countries = new List<string> { "Germany", "Poland", "France", "United States", "Russia", "Japan" };
    List<string> capitals = new List<string> { "berlin", "warsaw", "paris", "washington dc", "moscow", "tokyo" };

    int score = 0;
    Random rnd = new Random();

    while(countries.Count > 0) {
        int random_number = rnd.Next(0, countries.Count);

        string current_country = countries[random_number];
        string current_capital = capitals[random_number];

        Console.WriteLine("What is the capital of {0}?", current_country);

        string capital_input = Console.ReadLine();

        if (!string.IsNullOrWhiteSpace(capital_input) && capital_input.Equals(current_capital, StringComparison.InvariantCultureIgnoreCase)) {
            Console.WriteLine("Correct!");
            score++;
        } else {
            Console.WriteLine("Incorrect!");
        }
        countries.RemoveAt(random_number);
        capitals.RemoveAt(random_number);
    }

    int score_percent = score * 20;
    Console.WriteLine("Congratulations! You scored {0}% of questions correct.", score_percent);
    Console.ReadKey();
}

现在我尝试解释一些案例(也使用评论):

  1. 您无法使用Remove[random_number]从数组中删除元素。正如所说的J.Skeet:删除是一种方法,而不是索引器
  2. 最好使用List<string>代替。然后,您可以删除RemoveAt(random_number)
  3. 等元素
  4. 您在宣布之前使用random_number
  5. 不要在循环中声明Random
  6. 在这种情况下,更优选使用while代替for
  7. 使用忽略案例检查输入资本