Main()方法必须有一个返回类型错误;第一天搞乱与C## [英] Main() method must have a return type error; first day messing w/ C#

查看:149
本文介绍了Main()方法必须有一个返回类型错误;第一天搞乱与C##的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用C#,并且在我的班级作业的一部分中遇到过这个错误。不幸的是,我现在的代码是文盲,所以请耐心等待我。基于下面的代码,我知道我正在编译错误的东西,但我不明白我做错了什么并且继续得到CS1520方法必须有一个返回类型错误并且无法找出原因。



编辑:向静态void添加void Main()产生CS0017错误定义了多个入口点。再一次,我不明白这意味着什么,或者我应该解决什么。



我尝试过:



I'm just learning how to use C# and have been faced with this error as part of an assignment for my class. Unfortunately, I'm code illiterate at the moment, so please be patient with me. Based on the code below, I know that I'm compiling something wrong, but I don't understand what I'm doing wrong and keep getting a CS1520 "Method must have a return type" error and can't figure out why.

Adding void to static void Main() produces a CS0017 error of "More than one entry point defined". Yet again, I don't understand what this means, or what I should fix.

What I have tried:

using System;
using System.Linq;

class Program
{
    static Main()
    {
        int number1, number2, number3;
        Console.Write("Enter an integer score");
        number1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter an integer score");
        number2 = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter an integer score");
        number3 = Convert.ToInt32(Console.ReadLine());

        int result = (number1 + number2 + number3) % 3;
        Console.WriteLine("The average of {0}, {1}, {2} is: {3}",
        number1, number2, number3, result);
    }
}

推荐答案

C#中的每个方法都需要返回类型。将您的方法原型更改为此



static void main( < b> string [] args
You need a return type on every method in C#. Change your method prototype to this

static void main(string[] args)


John为您提供了编译问题的解决方案。

请注意您还有一个逻辑错误:

John gave you the solution to the compilation problem.
Please note you have also a logical error:
Quote:

int result = (number1 + number2 + number3)%3;

int result = (number1 + number2 + number3) % 3;

这不计算三个整数的平均值。你应该写:

That's not compute the average of the three integers. You should write instead:

double avg = (number1 + number2 + number3) / 3.0;


这是非常友好的建议!



越早查找并解决自己的错误越多越好。现在,在开始时更是如此。它应该超越习惯 - 反射。



它可以帮助你更快地了解你正在做的事情。就像在纸质词典的时代。你可以只查找你想要的单词 - 但你也可以看看它之前和之后的单词 - 出于好奇。



在上面,你真的需要了解更多关于返回类型的信息,因为它们在所有类型的计算机语言中都非常重要。



真的 - 进入查找并然后超越。用于计算机编程和其他一切。它是一生中平庸之路的一部分,只是做了最低限度。
This is intended to be very friendly advice!

The sooner you get into looking up and solving your own errors the better. Even more so, now, in the beginning. It should become beyond habit - a reflex.

It offers the possibility of helping you learn what you're doing faster. Like in the days of paper dictionaries. You could just look up the word you wanted - but you could also look at the words before and after it, as well - out of curiosity.

In the above, you really need to learn more about "return types" as they are absolutely deadly important in all typed computer languages.

Really - get into the lookup and then look beyond. For computer programming and everything else. It's a part of the route out of a lifetime of mediocrity that follows doing just the bare minimum.


这篇关于Main()方法必须有一个返回类型错误;第一天搞乱与C##的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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