我正在尝试使用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
有什么想法吗?
答案 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();
}
现在我尝试解释一些案例(也使用评论):
Remove[random_number]
从数组中删除元素。正如所说的J.Skeet:删除是一种方法,而不是索引器List<string>
代替。然后,您可以删除RemoveAt(random_number)
random_number
。Random
。while
代替for