学习c#并试图创建一个快速游戏,我得到的错误信息"消息"是一个"变量",但像一个"方法"使用。创建了一个名为message的空字符串,并在下面定义了它。以下是微软虚拟学院的教程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrizeGame
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, you're going to play a game"+
"Pick a number between 1 and 10, Type it and press enter.");
string userValue = Console.ReadLine();
string message="";
string noWin ="Sorry, you didn't win anything :(. On the"+
"other hand you helped me with my project, thanks!";
if (userValue == "1")
{
message(noWin);
}
else if (userValue=="2")
{
message("Congratulations, you won a penny! Also, thanks for"+
"helping me with my project!");
}
else if(userValue=="3")
{
message(noWin);
}
else if(userValue=="3")
{
message(noWin);
}
else if(userValue=="4")
{
message("Congratulations, you won 20 pence! Also, thanks for"+
"helping me with my project!");
}
else if(userValue=="5")
{
message(noWin);
}
else if(userValue=="6")
{
message(noWin);
}
else if(userValue=="7")
{
message ("Congratulations, you just won the top prize, £1!!"+
"Also, thanks for helping me with my project.");
}
else if (userValue=="8")
{
message(noWin);
}
else if (userValue=="9")
{
message(noWin);
}
else if (userValue=="10")
{
message(noWin);
}
else
{
message("Sorry I couldn't understand what you wrote"+
"make sure you use a number between 1 and 10");
}
Console.WriteLine(message);
Console.ReadLine(),;
}
}
}
`
您正在声明一个名为message的字符串变量。你不能用方法符号来调用它。
这样做
message = "Congratulations, you won a penny! Also, thanks for"+"helping me with my project!";
这是一个函数调用,你有什么:
message(noWin);
是一个字符串赋值:
message = noWin;
或
message += noWin; //appended to the end
您希望后者之一将值赋给变量,以供以后使用;c#不使用()符号。如果您希望消息是一个数组,那么您需要:
string[] message;
但是,这样做要容易得多:
var messages = new List<string>();
messages.Add(noWin);
我看过你的代码,在。net中,你有一个方法,或者一个变量,一个变量是由它的名字调用的,而一个函数或函数是由它的名字调用的,后面跟着()。括号之间可以是函数的参数。
你的消息没有任何东西,而你的noWin是一个字符串/文本。
当你想显示该字符串时,你应该使用:
Console.WriteLine(noWin);
也许你应该这样使用:
MessageBox.Show(noWin);
在你的情况下,使用switch语句而不是if else if会更容易读懂。