为什么我得到“方法必须有一个返回类型”
本文关键字:有一个 返回类型 方法 为什么 | 更新日期: 2023-09-27 18:12:00
所以,今天下午我开始尝试学习c#。我一直在玩辐射避难所,我很想制作我自己的居民荒地探索部分的碳副本,然后我最终可以调整和扩展。
我有Program.cs, Speech.cs和Events.cs
program.cs调用speech.cs每个"回合"显示随机的句子。然后它意味着调用events.cs来确定每个"回合"的事件类型。
问题是我似乎不能得到事件方法的工作。它一直在说方法必须有一个返回类型
Speech方法似乎工作得很好,但是,回顾它,我不认为我真的理解我做了什么。
如有任何帮助,不胜感激。
下面的代码。
Program.cs
using System;
namespace Roaming
{
class MainClass
{
public static void Main (string[] args)
{
//Declare Variables
int milliseconds = 1000;
int currenthp = 100;
int maxhp = 100;
string eventGet;
do {
Console.WriteLine("Your HP is at {0}/{1} 'n", currenthp, maxhp);
Random random = new Random();
int randomNumber = random.Next(0,100);//Random roll do determine event
if(randomNumber > 50)
{
Roaming.typeOfEvent();
}
else
{
Console.WriteLine ( Speech.GetRandom() );//Random Speech
}
System.Threading.Thread.Sleep (milliseconds);//Timer Delay
Console.Clear();
} while(currenthp > 0);
}
}
}
Speech.cs
using System;
using System.Collections.Generic;
namespace Roaming
{
public class Speech
{
private static readonly List<string> sentences = new List<string>()
{
"It's so cold out here.",
"I don't know where to go.",
"Is that a finger?",
"I have a pain in my Stomach.",
"I'm sure I saw something move.",
"Saw a Guard Dog, watching the area.",
"I'm bleeding a little.",
"I feel a little faint.",
"I should have brought a book.",
"How are things back at base I wonder.",
"I wonder what's over there.",
"I want to head back soon.",
"Theres a lot of noise coming from over there.",
"I think there's the remnants of what was once a village over there.",
"Are Feral Ghouls real?",
"Theres something wirtten in the wall ehre but I can't make it out.",
"I can see a RadScorpion",
"I think I'm lost.",
"I wonder if the radiation has created Zombies.",
"I am so hungry.",
"Anyone nearby could probably hear my stomach rumbling.",
"Ooh, that was a good fart.",
"Could really do with some water.",
"There's something strange moving in the sky."
};
private static readonly Random rangomGenerator = new Random();
public static string GetRandom()
{
int max = sentences.Count-1;
int randomNumber = rangomGenerator.Next(max);
return sentences[randomNumber];
}
}
}
Events.cs
using System;
using System.Collections.Generic;
namespace Roaming
{
public class eventType
{
public static typeOfEvent(string eventGet)
{
//Determining the type of event
Random random = new Random();
int randomNumber = random.Next(0,100);//Random roll do determine event
if(randomNumber > 95)
{
Console.WriteLine("Find Weapon.");
}
else if(randomNumber > 90)
{
Console.WriteLine("You found a stimpak");
}
else if(randomNumber > 70)
{
Console.WriteLine("You were attacked");
}
else
{
Console.WriteLine("You continue to explore");
}
}
}
}
正如所述,您需要一个返回类型,如果您不返回任何内容,则应该是void
。
public static void typeOfEvent(string eventGet)
按如下所示修改签名
public static void typepeofevent (string eventGet)