【问题标题】:C# Beginner Question about UserInput in a Method [duplicate]关于方法中的用户输入的C#初学者问题[重复]
【发布时间】:2021-09-18 20:27:31
【问题描述】:

我是编程新手,正在尝试做第一个程序。

我想使用用户输入的方法来更改 Main 函数中的变量。 但我得到错误:“成员'float.Parse(字符串)'不能通过实例引用访问”

这是我的代码

                    
public class Program
{
    public static void Main()
    {   float salary = 0;
        float rentalFee;
        float powerCosts;
        float gez;
        bool gezMonth;
        float insurence;
        bool insurenceMonth;
        userOutput("Geben sie einen Wert für ihr Gehalt ein");
        salary = UserInput(Console.ReadLine());
        Console.WriteLine(salary);
        
    }
    private static float UserInput(string usrInput)
    {   
        float input= 0;
        input.Parse(usrInput);
        return input;
    }

我用谷歌搜索了错误,但我真的不明白答案:D 这是因为它的公众吗? 我应该直接在 Main 函数中执行 UserInputs 吗?

【问题讨论】:

  • 试试float input = float.Parse(usrInput);Parse 是一个静态方法,只能从类型而不是实例中静态调用。

标签: c# parsing methods member


【解决方案1】:

使用

input = float.Parse(usrInput);

而不是

input.Parse(usrInput);

请参阅How to convert a string to a number (C# Programming Guide)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2011-01-12
    • 2018-01-08
    • 2013-06-28
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多