簡體   English   中英

為什么我收到“方法必須有一個返回類型”?

[英]Why am I getting "Method must have a return type"?

所以,我今天下午開始嘗試學習 C#。 我一直在玩 Fallout Shelter 並且很想制作我自己的 Dwellers Wasteland Exploration 部分的 Carbon 副本,然后我可以最終調整和擴展。

我有 Program.cs、Speech.cs 和 Events.cs

program.cs 調用 Speech.cs 來顯示每個“回合”的隨機句子。 然后它意味着調用 events.cs 來確定每個“回合”的事件類型。

問題是我似乎無法讓事件方法起作用。 它一直說 Method 必須有一個返回類型。

Speech 方法似乎工作得很好,但回想起來,我認為我並沒有真正理解我所做的。

任何幫助,將不勝感激。

代碼如下。

程序.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];
        }


    }
}

事件.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 typeOfEvent(string eventGet)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM