פורסם 2011 ביוני 2114 שנים יש לי תרגיל שבו אני מכניס סטרינג מסויים ואמור לקבל את הפלט כ-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(); } } }
פורסם 2011 ביוני 2114 שנים אין שום בעיה לעשות פונקציה שמחזירה מערך - אתה רק צריך לציין את ערך ההחזרה המתאים בחתימה של הפונקציה. במקרה שלך, ערך ההחזרה הוא int, אז אתה צריך לשנות אותו למערך של int.
פורסם 2011 ביוני 2114 שנים תעלה לכאן את הקוד שניסית לכתוב (מה זה return array? למערך שלך קוראים NumArray).
פורסם 2011 ביוני 2114 שנים מחבר 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[]
פורסם 2011 ביוני 2114 שנים הבעיה היא לא במתודה, אלא בהדפסה בסוף...אתה צריך להדפיס כך: for (int i = 0; i < 3; i++){ Console.WriteLine(result[i]);}כלומר, לפנות לתא המתאים במערך, ולא להדפיס את המשתנה של המערך עצמו.
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.