简体   繁体   English

通过索引从数组中删除字符串

[英]Remove string from array by index

I'm trying to build a simple country capitals quiz program using c#. 我正在尝试使用C#构建一个简单的国家首都测验程序。 Currently, I am giving the user a random country from the countries list and asking for input on what the capital is. 目前,我正在从国家/地区列表中为用户提供一个随机国家/地区,并要求输入什么是首都。 Then there is a conditional that checks if the correct capital is equal to the input. 然后有一个条件检查正确的资本是否等于输入。 After this completes, I need to be able to remove the country and capital asked from each array so that the loop does not repeat them. 完成此操作后,我需要能够从每个数组中删除所要求的国家和资本,以便循环不会重复它们。 Here is the code I have so far: 这是我到目前为止的代码:

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);
  }
}

} }

The program fails to compile with these errors: 该程序无法编译并出现以下错误:

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

Any ideas? 有任何想法吗?

Consider using the following: 考虑使用以下内容:

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

There are many issues in your code. 您的代码中有很多问题。 If you want working code then use this: 如果您想要工作代码,请使用以下代码:

 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();
}

Now I try to explain some cases (also using comments): 现在,我尝试解释一些情况(也使用注释):

  1. You can't remove elements from arrays using Remove[random_number] . 您不能使用Remove[random_number]从数组中删除元素。 As said J.Skeet: Remove is a method, not an indexer 正如J.Skeet所说:Remove是一种方法,而不是索引器
  2. It would be better to use List<string> instead. 最好使用List<string>代替。 Then you can remove elements like RemoveAt(random_number) 然后,您可以删除元素,如RemoveAt(random_number)
  3. You're using random_number before you've declared it. 在声明之前,您正在使用random_number
  4. Don't declare the Random in the loop. 不要在循环中声明Random
  5. In this case using while instead of for is more preferable 在这种情况下for更优选使用while代替for
  6. Check of input capital with ignore case 忽略大小写检查输入资金

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM