C#ConsoleWriteLine,ReadLine错误?

时间:2018-06-15 08:49:17

标签: c#

问题是我无法使用所有这些简单的东西,我也导入了所有这些库......

using System;
using System.Collections.Generic;    
using System.Linq;    
using System.Text;    
using System.Threading.Tasks;    
using System.Diagnostics;

public class GainMoney
{

    public int currentMoney = 100;
    public string choose;

    Console.WriteLine("Please Choose What Are You Wanna Produce");
    choose = Console.ReadLine();



    public int addCurrentMoney()
    {
        if ((currentMoney - Farm.costoffarm) > 0)
        {
            System.Threading.Thread.Sleep(50000);
            return currentMoney + 10;
        }
        else
        {
            return currentMoney;
        }

    }
}

ERROR LIST --->

3 个答案:

答案 0 :(得分:2)

你不能在课堂上调用函数(方法,proeprties,事件等......)。

以下几行:

Console.WriteLine("Please Choose What Are You Wanna Produce");
choose = Console.ReadLine();

必须在方法内。

答案 1 :(得分:2)

Console.WriteLine("Please Choose What Are You Wanna Produce");

此语句必须位于执行方法内。正如@Markus Dresch所建议的那样 - "

  

你不能在课堂上调用函数(方法,proeprties,事件等......)   水平。

尝试修复您的代码:

using System;
using System.Collections.Generic;    
using System.Linq;    
using System.Text;    
using System.Threading.Tasks;    
using System.Diagnostics;

public class GainMoney
{   

    public int currentMoney = 100;
    public string choose;



    public int addCurrentMoney()
    {
        if ((currentMoney - Farm.costoffarm) > 0)
        {
            System.Threading.Thread.Sleep(50000);
            return currentMoney + 10;
        }
        else
        {
            return currentMoney;
        }

    }

    public void SomeNewMethod(){
        Console.WriteLine("Please Choose What Are You Wanna Produce");
        choose = Console.ReadLine();
    }
}

正如我假设您已创建控制台应用程序,请按如下方式使用该类:

using System;

public class Program
{
    public static void Main()
    {
        GainMoney gainMoney = new GainMoney();
        gainMoney.SomeMethod();
    }
}

答案 2 :(得分:1)

你应该写下这些行:

Console.WriteLine("Please Choose What Are You Wanna Produce");
choose = Console.ReadLine();

进入你的类的结构,当你希望每次都调用它们时。

像:

public class GainMoney
 {
    public int currentMoney = 100;
    public string choose;

    public GainMoney()
    {
       Console.WriteLine("Please Choose What Are You Wanna Produce");
       choose = Console.ReadLine();
     }
 }

编辑:向构造函数添加了访问修饰符public,错字