I'm trying to build a simple country capitals quiz program using 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?
List<string>instead. Next,Removeis a method, not an indexer - so you wantRemove(...)rather thanRemove[...].random_numberbefore you've declared it. So putcurrent_countryandcurrent_capitalinside the loop, belowrandom_numberNext()function inside the loop.