简体   繁体   中英

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

So, I started trying to learn C# this afternoon. I've been playing around with Fallout Shelter and would love to make my own Carbon copy of the Dwellers Wasteland Exploration section that I can then eventually tweak and expand.

I have Program.cs, Speech.cs and Events.cs

The program.cs calls on speech.cs to display random sentences each 'turn'. Then it's meant to call on events.cs to determine the type of event each 'turn'.

The problem is I can't seem to get the Events Method to work. It keeps saying Method must have a return type.

The Speech method seemed to work fine but, looking back on it, I don't think I really understood what I did.

Any help would be appreciated.

Code below.

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)

Modify the signature as given below

public static void typeOfEvent(string eventGet)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM