有一个错误表示'else if'和'else'
上的表达式无效using System;
namespace test
{
class MainClass
{
public static void Main (string[] args)
{
Random numberGenerator = new Random ();
int num01 = numberGenerator.Next (1,11);
int num02 = numberGenerator.Next (1,11);
int realAnswer = num01 * num02 ;
Console.WriteLine ("What is " + num01 + " multiplied by " + num02 + "?");
int userAnswer = Convert.ToInt32 (Console.ReadLine ());
if (userAnswer == realAnswer) {
Console.WriteLine ("Good");
} else if (userAnswer - realAnswer >= 1 && userAnswer - realAnswer <= 3) {
int greater = numberGenerator.Next (1, 3);
switch (greater) {
case 1:
Console.WriteLine ("little too high");
break;
default:
Console.WriteLine ("little too much");
break;
} else if (realAnswer-userAnswer >= 1 && realAnswer-userAnswer <=3) {
int less = numberGenerator.Next (1, 3);
switch (less) {
case 1:
Console.WriteLine ("little too low");
break;
default:
Console.WriteLine ("go higher!");
break;
} else {
Console.WriteLine ("you just suck");
}
Console.ReadKey ();
}
}
}
}
}
答案 0 :(得分:3)
在两个开关之后你都缺少结束大括号(}
):
if (userAnswer == realAnswer) {
Console.WriteLine ("Good");
} else if (userAnswer - realAnswer >= 1 && userAnswer - realAnswer <= 3) {
int greater = numberGenerator.Next (1, 3);
switch (greater) {
case 1:
Console.WriteLine ("little too high");
break;
default:
Console.WriteLine ("little too much");
break;
}
} // Was missing in the OP
else if (realAnswer-userAnswer >= 1 && realAnswer-userAnswer <=3) {
int less = numberGenerator.Next (1, 3);
switch (less) {
case 1:
Console.WriteLine ("little too low");
break;
default:
Console.WriteLine ("go higher!");
break;
}
} // Was also missing
else {
Console.WriteLine ("you just suck");
}
答案 1 :(得分:1)
在开关结束时忘记支撑:小心
public static void Main(string[] args)
{
Random numberGenerator = new Random ();
int num01 = numberGenerator.Next (1,11);
int num02 = numberGenerator.Next (1,11);
int realAnswer = num01 * num02 ;
Console.WriteLine ("What is " + num01 + " multiplied by " + num02 + "?");
int userAnswer = Convert.ToInt32 (Console.ReadLine ());
if (userAnswer == realAnswer) {
Console.WriteLine ("Good");
} else if (userAnswer - realAnswer >= 1 && userAnswer - realAnswer <= 3) {
int greater = numberGenerator.Next (1, 3);
switch (greater) {
case 1:
Console.WriteLine ("little too high");
break;
default:
Console.WriteLine ("little too much");
break;
}
} else if ((realAnswer-userAnswer >= 1) && (realAnswer-userAnswer <=3)) {
int less = numberGenerator.Next (1, 3);
switch (less) {
case 1:
Console.WriteLine ("little too low");
break;
default:
Console.WriteLine ("go higher!");
break;
}
} else {
Console.WriteLine ("you just suck");
}
Console.ReadKey ();
}