Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#控制台写入线,读线错误?_C# - Fatal编程技术网

C#控制台写入线,读线错误?

C#控制台写入线,读线错误?,c#,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;

问题是我不能使用所有这些简单的东西,而且我导入了所有这些库

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;
        }

    }
}
“>

您不能在类级别调用函数(方法、项目、事件等)

以下几行:

Console.WriteLine("Please Choose What Are You Wanna Produce");
choose = Console.ReadLine();
必须位于方法内部。

不能在类级别调用函数(方法、项目、事件等)

以下几行:

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

必须在一个方法中。

您应该写以下行:

Console.WriteLine("Please Choose What Are You Wanna Produce");
choose = Console.ReadLine();
当你想让他们每次都被叫来的时候,你就可以进入你的课堂

比如:


编辑:为构造函数添加了访问修饰符
public
,输入错误

您应该写以下行:

Console.WriteLine("Please Choose What Are You Wanna Produce");
choose = Console.ReadLine();
Console.WriteLine("Please Choose What Are You Wanna Produce");
当你想让他们每次都被叫来的时候,你就可以进入你的课堂

比如:

编辑:向构造函数添加访问修饰符
public
,输入错误

Console.WriteLine("Please Choose What Are You Wanna Produce");
此语句需要在方法内部执行。正如@Markus Dresch所建议的那样—

不能在类上调用函数(方法、项目、事件等) 水平

尝试如下方式修复代码:

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();
    }
}
此语句需要位于方法内部才能执行。正如@Markus Dresch所建议的那样—

不能在类上调用函数(方法、项目、事件等) 水平

尝试如下方式修复代码:

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();
    }
}


您的类定义无效,控制台操作需要在函数内部。您的意思是“我不能使用”"? 您的代码甚至无法编译。@SeM:D这就是他在图像中出现编译时错误的原因;-)@CodeNotFound抱歉,我看不到来自我工作场所的图像,imgur被阻止:)请不要发布太小而无法查看的屏幕截图,而是将实际错误消息作为文本发布。您的类定义无效,控制台操作需要在函数内部。您的意思是“我不能使用”?您的代码甚至无法编译。@SeM:D这就是他在图像中出现编译时错误的原因;-)@CodeNotFound抱歉,我看不到我工作场所的图像,imgur被阻止:)请不要发布太小而无法查看的屏幕截图,而是以文本形式发布实际错误消息。实际上,你可以,只要方法返回一个值,您就可以将它赋给某个变量。您可以将静态方法的结果赋给类变量。@SeM,您就可以将它赋给某个变量。部分正确。OP已经执行了
choose=Console.ReadLine(),但这不是编译,因为它不是成员声明的一部分。我think@CodeNotFound是的,我只是在评论Markus的回答——“不能在类级别调用函数(方法、proeprites、事件等)。:)@这取决于你所说的“电话”是什么意思。这里是关于执行的。类级别也表示类的根。定义成员时调用函数不是类级别,而是成员级别;-)实际上,只要方法返回一个值并将其分配给某个变量,您就可以这样做。您可以将静态方法的结果分配给类变量。@SeM,并将其分配给某个对象。部分正确。OP已经执行了
choose=Console.ReadLine(),但这不是编译,因为它不是成员声明的一部分。我think@CodeNotFound是的,我只是在评论Markus的回答——“不能在类级别调用函数(方法、proeprites、事件等)。:)@这取决于你所说的“电话”是什么意思。这里是关于执行的。类级别也表示类的根。定义成员时调用函数不是类级别而是成员级别;-)调用
Console.ReadLine()在构造函数内。我认为这是一个非常糟糕的主意在构造函数内。我认为这真是个坏主意。