עבור לתוכן

C# - החזרת מערך במתודה

Featured Replies

פורסם

יש לי תרגיל שבו אני מכניס סטרינג מסויים ואמור לקבל את הפלט כ-3 int's, התוכנית אמורה להיות כתובה במתודה.

רשמתי מתודה שעושה את כל זה ובסופה אני מקבל מערך שמכיל את המספרים שבסוף אני אמור להדפיס למסך, אני יודע שעכשיו הוא מחזיר סתם משהו אבל שרשמתי שהוא יחזיר את המערך שלי הייתי חייב לרשום אינדקס מסויים וזה לא עוזר לי כי אני רוצה להחזיר את כל המערך שלי.

איך אני יכול להציג את המערך ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace T266B
{
class Program
{


//define method
static int stDate(string date)
{
int Num = 0;
int ArrayIndex = 0;
string temp = "";


//define array
string[] stArray = new string[3];
int[] NumArray = new int[3];


//seperate the date to day, month, year, convert to int and put into array
for (int i = 0; i < date.Length; i++)
{
if (date[i] != '/' & date[i] != ' ')
{
temp += date[i];
}
else if (temp != " ")
{
int Number = int.Parse(temp);
NumArray[ArrayIndex] = Number;
Num = Number;
Number = 0;
temp = "";
ArrayIndex++;
}
}


return Num;
}


static void Main(string[] args)
{
//read date
Console.Write("Enter Date (dd/mm/yyyy): ");
string date = Console.ReadLine();


date = date + " ";


//call method
int result = stDate(date);


for (int i = 0; i < 3; i++)
{
Console.WriteLine(result);
}


Console.ReadLine();


}
}
}

פורסם

אין שום בעיה לעשות פונקציה שמחזירה מערך - אתה רק צריך לציין את ערך ההחזרה המתאים בחתימה של הפונקציה. במקרה שלך, ערך ההחזרה הוא int, אז אתה צריך לשנות אותו למערך של int.

פורסם
  • מחבר

גם את זה ניסיתי אבל אחרי ה-RETURN ARRAY הוא ציפה לערך כלשהו

פורסם

תעלה לכאן את הקוד שניסית לכתוב (מה זה return array? למערך שלך קוראים NumArray).

פורסם
  • מחבר

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace T266B
{
class Program
{


//define method
static int[] stDate(string date)
{
int Num = 0;
int ArrayIndex = 0;
string temp = "";


//define array
string[] stArray = new string[3];
int[] NumArray = new int[3];


//seperate the date to day, month, year, convert to int and put into array
for (int i = 0; i < date.Length; i++)
{
if (date[i] != '/' & date[i] != ' ')
{
temp += date[i];
}
else if (temp != " ")
{
int Number = int.Parse(temp);
NumArray[ArrayIndex] = Number;
Num = Number;
Number = 0;
temp = "";
ArrayIndex++;
}
}


return NumArray;
}


static void Main(string[] args)
{
//read date
Console.Write("Enter Date (dd/mm/yyyy): ");
string date = Console.ReadLine();


date = date + " ";


//call method
int[] result = stDate(date);


for (int i = 0; i < 3; i++)
{
Console.WriteLine(result);
}


Console.ReadLine();


}
}
}

התוצאה שאני מקבל אחרי הכנסת תאריך מסויים בפורמט dd/mm/yyyy היא:

System.Int32[]

System.Int32[]

System.Int32[]

פורסם

הבעיה היא לא במתודה, אלא בהדפסה בסוף...

אתה צריך להדפיס כך:

      
for (int i = 0; i < 3; i++)
{
Console.WriteLine(result[i]);
}

כלומר, לפנות לתא המתאים במערך, ולא להדפיס את המשתנה של המערך עצמו.

פורסם
  • מחבר

תודה רבה.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים