-6

我正在尝试在 c# 中创建一个函数,但我似乎无法找到代码的问题所在。我知道这是一个非常基本的问题,但我真的需要你们的帮助!

这是我的代码:

public PlayerCards(string player1C1)
{
    string p1C1 = player1C1;
    return p1C1;
}

这些是我得到的错误:

错误 1 ​​方法必须有返回类型

错误 2 由于_2_Man_Hold_Em.GameLogic.PlayerCards(string)返回 void,return 关键字后面不能跟对象表达式

4

3 回答 3

1

Method must have a return type

Add a return type:

public string PlayerCards(string player1C1)

The other error goes away, too.

于 2013-05-11T22:17:52.727 回答
1

The first error can help you search for "C# methods", which will lead you to this:

Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.

The error says:

must have a return type

Searching "return type" on that page gives you:

If the return type, the type listed before the method name, is not void, the method can return the value by using the return keyword.

So between the access modifier "public" and the method name "PlayerCards" you need a "return type": string. It's all about definitions. If you get those, the compiler is really helpful with its errors.

于 2013-05-11T22:23:27.180 回答
0

You will solve this issue by adding a return type to the method if you wish to return a value.

public string PlayerCard(string card)

If you should add void to the method if you do not wish to return any value.

public void ProcessCard(string card)

Here are some resources which might be of help to you:

C# Fundamentals: Development for Absolute Beginners -- Set of some good video on C# programming fundamentals

C# Tutorials

于 2013-05-11T22:24:38.807 回答