无法从方法组转换为Int32 [英] Cannot convert from method group to Int32

查看:207
本文介绍了无法从方法组转换为Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的小型数学程序看起来非常时尚,这意味着在Main方法下,我可以使用以下方法:

I want my small math program to look really sleek, and by this I mean under the Main method I have the following methods:

Greet()
UserInput1()
UserInput2()
Result()

Greet()中我只说"HI",在UserInput1()中我要收集第一个数字,在UserInput2()中我要收集第二个数字,在Result()中我要打印的结果UserInput1 + UserInput2.我可以在UserInput 1 and 2中收集数字,但是如果不通过Main()函数为它们分配值,我似乎无法将它们发送到Result().

In Greet() I just say "HI", in UserInput1() I want to collect the first number, in UserInput2() I want to collect the second number, and in Result() I want to print the result of UserInput1 + UserInput2. I can collect the number in UserInput 1 and 2 but I can’t seem to send them to Result() without assigning values to them under the Main() function.

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Greet();
            firstNumber();
            secondNumber();
            result(firstNumber, secondNumber);
            Console.ReadKey();
        }

        public static void Greet()
        {
            Console.WriteLine("Hello, pls insert two numbers");
        }

        public static int firstNumber()
        {
            int num01 = Convert.ToInt32(Console.ReadLine());
            return num01;
        }

        public static int secondNumber()
        {
            int num02 = Convert.ToInt32(Console.ReadLine());
            return num02;
        }

        public static void result( int num01, int num02)
        {

            Console.WriteLine(num01 + num02);
        }
    }
}

推荐答案

更改此内容:

result(firstNumber, secondNumber);

对此:

result(firstNumber(), secondNumber());

并删除上面两行中对这两个方法的调用.

and remove the calls to the 2 methods in the two lines above.

要调用不带参数的方法,需要带括号的内容.

To call a method without parameters, you need the parentheses without content.

这篇关于无法从方法组转换为Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆