如何添加语句来调用您在类中声明的每个方法? [英] How do i add statements to call every method you declared in class?

查看:55
本文介绍了如何添加语句来调用您在类中声明的每个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace Assignment4_2
{
    class SumArray
    {
        public class ProjectArray_Nonyelu
        {
            private int[] data;
            
            public void SumArray(int[] d)
            {
                data = d;
            }

            public int[] SumOfArray()
            {
                Console.Write("Enter an array size: ");
                int size = int.Parse(Console.ReadLine());

                //int i = 0, sum = 0;
                int[] anArray = new int[size];

                for (int i = 0; i < size; i++)
                    //sum += anArray[i];
                {
                    Console.Write("anArray[" + i + "] =  " );
                    anArray[i] = int.Parse(Console.ReadLine());
                }
                return anArray;
            }

            public static int[] SumOfArray(int[] d)
            {
                int[] a = { 5, 4, 6, 8 };

                int x = 0, sum = 0;
                for (int i = 0; i < a.Length; i++)
                    sum += a[i];
                {
                    Console.WriteLine(sum);
                }

                return a;
            }

            public int MaxOfArray()
            {
                int[] c = new int[5];
                int max;

                for (int i = 0; i < 5; i++)
                {
                    c[i] = int.Parse(Console.ReadLine());
                }
                max = c[0];

                for (int i = 0; i < 5; i++)
                {
                    if(c[i] > max)
                    {
                        max = c[i];
                    }
                }
                return max;
            }

            public static int[] MaxOfArray(int[] a)
            {
                int[] b = new int[5];
                int max;

                Console.Write("Enter 5 numbers: ");
                for (int i = 0; i < 5; i++)
                {
                    b[i] = int.Parse(Console.ReadLine());
                }

                max = b[0];

                for (int i = 0; i < 5; i++)
                {
                    if(b[i] > max)
                    {
                        max = b[i];
                    }
                }
                //return b;
                Console.WriteLine("The largest number is {0}", max);
                return b;
            }

            public void DisplayArray(String m)
            {
                Console.Write(m + "{");
                for (int i = 0; i < data.Length; i++)
                {
                    if(i != 0)
                        Console.Write(",");
                    Console.Write(data[i]);
                }
                Console.WriteLine("}");
            }

            public static void DisplayArray()
            {
                int[] a = { 3, 4, 5, 6 };
                Console.Write("{");
                for (int i = 0; i < a.Length; i++)
                {
                    Console.WriteLine(a);
                }
            }

            public static int[] ReadIntArray()
            {
                Console.WriteLine("Enter an array size:");
                int size = int.Parse(Console.ReadLine());

                int[] anArray = new int[size];

                for (int i = 0; i < size; i++)
                {
                    Console.WriteLine("anArray [" + i + "] = ");
                    anArray[i] = int.Parse(Console.ReadLine());
                }

                return anArray;
            }
        }

        public class TestProjectArray_Nonyelu
        {

        }
        static void Main(string[] args)
        {
            /*TestProjectArray_Nonyelu();
            SumOfArray();
            MaxOfArray();
            DisplayArray();
            ReadIntArray();*/
            
        }
    }
}





我尝试了什么:



我试图调用所有方法,但我一直收到错误。我已经在主要部分取消了它。



我发布了作业的指示,所以你可以看到我编写代码的方式。如果你能告诉我我的错误,我真的很感激。



创建一个控制台应用程序Assignment4_YourLastName,然后将两个类ProjectArray_yourLastName和TestProjectArray_yourLastName添加到你的项目中。 br />
I.使你的类ProjectArray_yourLastName具有:

1.一个int数组类型的私有变量数据;

2.一个带有一个参数的构造函数初始化数据;

3.实例方法SumOfArray返回数据元素的总和;

4.静态方法SumOfArray,它接受int数组类型的一个参数并返回其参数元素的总和;

5.实例方法MaxOfArray返回数据元素的最大元素;

6.静态方法MaxOfArray采用int数组的一个参数输入并返回其参数元素的最大元素;

7.显示消息和数据的实例方法DisplayArray

8.静态方法od显示消息和数组的DisplayArray

9.读取数组并返回数组的静态方法ReadIntArray



II。使您的类TestProjectArray_yourLastName只有Main方法。在Main方法中,添加语句以至少调用一次在ProjectArray_yourLastName类中声明的每个方法。注意:有实例方法和静态方法。它们以不同的方式被声明和使用!!!!!!!



What I have tried:

I've tried to call all of the methods, but I keep getting errors. I've cancelled it out in the main.

I posted the directions of the assignment, so you can see how I wrote my code. If you can let me know my mistakes I'd really appreciate it.

Create a console application, Assignment4_YourLastName, then add two classes ProjectArray_yourLastName and TestProjectArray_yourLastName to your project.
I. Make your class ProjectArray_yourLastName have:
1. a private variable data of int array type;
2. a constructor which takes one parameter to initialize data;
3. an instance method SumOfArray which returns the sum of data elements;
4. a static method SumOfArray which takes one parameter of int array type and returns the sum of its parameter’s elements;
5. an instance methods MaxOfArray which returns the maximum element of data elements;
6. an static methods MaxOfArray takes one parameter of int array type and returns the maximum element of its parameter’s elements;
7. an instance method DisplayArray of displaying a message and data
8. a static method DisplayArray of displaying a message and an array
9. a static method ReadIntArray of reading an array and returning the array

II. Make your class TestProjectArray_yourLastName only has the Main method. In the Main method, add statements to call every method you declared in class ProjectArray_yourLastName at least once. Note: there are instance methods and static methods. They are declared and used in different ways!!!!!!!

推荐答案

我不喜欢把它全部用于作业问题,所以这里是一个提示:



看看这一行:

I'm not a fan of giving it all away for homework questions, so here's a hint:

Take a look at this line:
Console.WriteLine("anArray [" + i + "] = ");



你在那做什么?您正在调用类控制台的方法 WriteLine(..)。你是否必须创建一个 Console 的实例才能做到这一点?不 - 所以它必须是静态方法。当然,你必须写控制台。在它前面,否则编译器不会知道你打算在Console类上调用 WriteLine(..)而不是作为你的类的方法写在那里。


What are you doing there? You're calling the method WriteLine(..) of the class Console. Did you have to create an instance of Console to be able to do that? No - so it must be a static method. And, of course, you have to write "Console." in front of it, otherwise the compiler wouldn't know that you intend to call WriteLine(..) on the Console class and not as a method of the class you've written yourself there.


这篇关于如何添加语句来调用您在类中声明的每个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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