【发布时间】:2013-07-01 18:20:39
【问题描述】:
我正在尝试创建一个简单的程序来查找数组中的最大数。我在一个单独的类文件中创建了该方法,然后只是尝试在主页中创建该对象,并在我创建的数组上执行该方法。我知道这与我目前没有在我的方法上返回值有关,但我仍然卡住了。抱歉这个菜鸟问题,提前谢谢。
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;
namespace FindMax
{
class Program
{
public static void Main(string[] args)
{
Class1 MyClass = new Class1();
int[] myArray = new int[] {1, 3, 4, 2, 5, 2, 2, 6, 3344, 223, 35, 5656, 2, 355543, 2222, 2355, 933433};
int y = MyClass.FindMax(myArray);
Console.WriteLine(y);
Console.ReadKey(true);
}}}
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;
namespace FindMax
{
public class Class1
{
public int FindMax(int[] array)
{
int temp = array[0];
for (int i = 0; i < array.Length; i++)
{
if (array[i] > temp)
{
temp = array[i];
}
}}}}
【问题讨论】:
-
我在您的
FindMax()中根本没有看到return声明。这可能是一个开始。 -
您需要在
Class1.FindMax末尾添加return temp;。 -
是的,在我班上第二个括号之后的“返回温度”是阻碍我的原因。感谢大家帮助我解决我的菜鸟问题!
-
@user2411290 - 您应该接受以下答案之一。
标签: c# arrays methods return-value