我刚刚进入编程,尝试了所有的东西,但第一个其他的总是无效的,它的probs真的很明显,但无法弄明白
if (answer == (num1 * num2))
{
int responsegen = numbergenerator.Next (1, 4);
switch (responsegen)
{
case 1:
Console.WriteLine ("Congrats! You're a natrual");
break;
case 2:
Console.WriteLine ("Correct, Mini Einstein maybe?");
default :
Console.WriteLine ("Right answer , are you cheating?");
}
else // this one right here
{
int diff = (answer - (num1 * num2));
if (diff == 1)
{
Console.WriteLine ("OHH so close you were only of by one number!");
}
if (diff <=10)
{
Console.WriteLine ("Incorrect but you were close, only of by " + diff + "better luck next time!");
}
}
答案 0 :(得分:1)
你正试图这样做:
switch (responsegen) {
//...
} else
//...
您无法使用switch
关注else
语句。 switch
在其案例列表中使用default
子句作为逻辑“else”。 (你已经在使用它,所以你所做的甚至在语义上都没有意义。)
也许您打算将else
附加到前一个if
块?:
if (answer == (num1 * num2)) {
//...
switch (responsegen) {
//...
} // <-- first close the switch block
} else // <-- then continue the if block
//...
当你继续学习编程时,你会发现使用空格的一致和干净的使用(回车,缩进等)会使这个很多更容易找到。
答案 1 :(得分:0)
你错过了一个大括号和几句break;
陈述:
if (answer == (num1 * num2))
{
int responsegen = numbergenerator.Next(1, 4);
switch (responsegen)
{
case 1:
Console.WriteLine("Congrats! You're a natural");
break;
case 2:
Console.WriteLine("Correct, Mini Einstein maybe?");
break; // <-- you need a break; here...
default:
Console.WriteLine("Right answer, are you cheating?");
break; // <-- ...and here.
} // <-- add a brace here
}
else
{
int diff = answer - (num1 * num2);
if (diff == 1)
{
Console.WriteLine("OHH so close, you were only off by one number!");
}
else if (diff <= 10)
{
Console.WriteLine("Incorrect but you were close - only off by " + diff + ". Better luck next time!");
}
}
我已经更正了你的拼写和语法,作为奖励。
答案 2 :(得分:0)
你缺少2个休息语句和大括号。
工作代码:
if (answer == (num1*num2))
{
int responsegen = numbergenerator.Next(1, 4);
switch (responsegen)
{
case 1:
Console.WriteLine("Congrats! You're a natrual");
break;
case 2:
Console.WriteLine("Correct, Mini Einstein maybe?");
break;
default:
Console.WriteLine("Right answer , are you cheating?");
break;
}
}
else // this mofo right here help!!!!
{
int diff = (answer - (num1*num2));
if (diff == 1)
{
Console.WriteLine("OHH so close you were only of by one number!");
}
if (diff <= 10)
{
Console.WriteLine("Incorrect but you were close, only of by " + diff + "better luck next time!");
}
}
此外,即使一旦编译,if语句也有点怀疑......如果'diff'为1,那么你将得到两条写入控制台的消息......