פורסם 2011 ביולי 2614 שנים יש לי תרגיל שכמעט וגמרתי אותו, בתרגיל אני אמור ליצור כמה צורות (משולש מלבן או מעגל) וכל מיני מתודות שמטפלות בכל צורה (מזיזים, יוצרים וכד')בסוף התרגיל ממש לפני ה-MAIN אני אמור ליצור מתודה שה-MAIN קורא מהמשתמש מספר בין 1 ל-4 , כל מספר בין 1 ל-3 מייצג צורה מסויימת (משולש מלבן או מעגל והמספר 4 זה יציאה מהלולאה) ואז המשתמש מכניס את הנתונים (נק' מוצא על הצירים גודל וכד') והמתודה לוקחת את האובייקט של הצורה ומכניסה אותו למערך וככה כל הצורות נשמרות לי במערך.יצרתי לדוגמה ב-MAIN צורה ראשונה (כפי שאתם רואים מעגל) ומתודה שלוקחת את האובייקט שנוצר ומכניסה אותו למערך ובסופו של דבר מחזירה את אותו מערך אבל יש לי בעיה ב-MAIN אחרי יצירת הצורה לקרוא למתודה ולשלוח אליה את האובייקט, אני פשוט לא יודע איך לקרוא למתודהת אשמח לעזרהתודה.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Shape{ public class Point { private float x; private float y; // Definition of c'tors public Point(float a, float b) { this.x = a; this.y = b; } public Point() { this.x = 0; this.y = 0; } public Point(Point p) { this.x = p.x; this.y = p.y; } // Definition of the methods public void Setx(float x) { this.x = x; } public void Sety(float y) { this.y = y; } public void Setpoint(Point p) { this.x = p.x; this.y = p.y; } public float Getx() { return this.x; } public float Gety() { return this.y; } public string ToString() { string result = "X= " + this.x + " Y= " + this.y; return result; } public void Move(float dx, float dy) { this.x += dx; this.y += dy; } } public class Shape { protected Point position; protected int color; protected float boardWidth; // Definition of c'tors public Shape() { this.position = new Point(); this.color = 1; this.boardWidth = 0; } public Shape(Point pos, int col, float bw) { this.position = pos; //option 1 this.position = new Point(pos); //option 2 this.color = col; this.boardWidth = bw; } public Shape(Point pos) { this.position = new Point(pos); this.color = 1; this.boardWidth = 0; } // Definition of the methods public string ToString() { string result = this.position.ToString(); result += " Color: " + this.color + " Board-width: " + this.boardWidth; return result; } public void Move(float dx, float dy) { //dx += this.position.Getx(); //dy += this.position.Gety(); //this.position.Setx(dx); //this.position.Sety(dy); //option 2 this.position.Move(dx, dy); } } public class Rectangle : Shape { private int width; private int height; // Definition of c'tors public Rectangle(Point pos, int col, float bw, int width, int height) : base(pos, col, bw) { this.width = width; this.height = height; } public Rectangle(Point pos, int width, int height) : base(pos) { this.width = width; this.height = height; } public string ToString() { return "Rectangle info:" + this.position.ToString() + this.width + " " + this.height; } } public class Circle : Shape { private int radius; // Definition of c'tors public Circle(Point pos, int col, float bw, int radius) : base(pos, col, bw) { this.radius = radius; } public Circle(Point pos, int radius) : base(pos) { this.radius = radius; } public string ToString() { return base.ToString() + " circle info:" + this.radius; } } public class Triangle : Shape { private Point p2; private Point p3; // Definition of c'tors public Triangle(Point pos, int col, float bw, Point p2, Point p3) : base(pos, col, bw) { this.p2 = new Point(p2); this.p3 = new Point(p3); } public Triangle(Point pos, Point p2, Point p3) : base(pos) { this.p2 = new Point(p2); this.p3 = new Point(p3); } } class Program { public Shape[] ShapeIntoArray(Shape a ) { Shape[] ShapeArray = new Shape[2]; for (int i = 0; i < 2; i++) { ShapeArray[i] = a; } return ShapeArray; } static void Main(string[] args) { Console.WriteLine("Choose Shape: (1: Circle, 2: Rectangle, 3: Triangle, 4: exit)"); int Input = int.Parse(Console.ReadLine()); while (Input != 4) { switch (Input) { case 1: Console.WriteLine("Enter The Position: "); Console.Write("x: "); float x = float.Parse(Console.ReadLine()); Console.Write("Y: "); float y = float.Parse(Console.ReadLine()); Point p1 = new Point(x, y); Console.WriteLine("Enter Color: "); int color = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Radius: "); int radius = int.Parse(Console.ReadLine()); Console.WriteLine("Enter BW: "); float bw = float.Parse(Console.ReadLine()); Circle c1 = new Circle(p1, color, bw, radius); Shape s1 = new Circle(p1, color, bw, radius); default: break; } } } }}
פורסם 2011 ביולי 2614 שנים שים לב שהמתודה Main היא סטטית! static void ....ולכן היא יכולה לפנות באופן ישיר רק למתודות שגם הן סטטיות!כלומר: public static Shape[] ShapeIntoArray(Shape a)במקום: public Shape[] ShapeIntoArray(Shape a)ואחרי זה תוכל לקרוא למתודה ShapeIntoArray כרגיל..בהצלחה
פורסם 2011 ביולי 2614 שנים בעקרון כדי שתוכל לקרוא למתודה ShapeIntoArray מתוך הMain כל מה שאתה צריך לעשות זה להוסיף את המילה static לחתימה של המתודה.ז"א לכתוב כך: public static Shape[] ShapeIntoArray(Shape a)למה צריך static? כיוון שהMain מוגדרת כstatic היא יכולה להפעיל רק מתודות שגם הן מוגדרות כstaticיש בMSDN הסבר מפורט יותר על מחלקות ומתודות סטטיות:http://msdn.microsoft.com/en-us/library/79b3xss3.aspx#Y1333עוד דוגמה והסבר טוב יש גם כאן: http://www.dotnetperls.com/static-method
פורסם 2011 ביולי 2614 שנים מחבר הבנתי תודה אבל אני עדיין לא מצליח לשלוח Shape למתודה, ב-MAIN יצרתי Shape שנקרא s1 ואני לא מצליח לשלוח אותו למתודה, רשמתי ShapeIntoArray(s1) ועוד כל מיני נסיונות וזה לא הולך.
פורסם 2011 ביולי 2714 שנים אם אתה מנסה לקרוא למתודה shapeintoarray עם s1 מחוץ לswitch זה לא ילך כי אחרי הswitchs1 לא קיים (בגלל שהוא מוגדר רק בתוך הswitch)אז או שתקרא למתודה מתוך הswitch או שתגדיר את s1 מחוץ לswitch static void Main(string[] args){Console.WriteLine("Choose Shape: (1: Circle, 2: Rectangle, 3: Triangle, 4: exit)");int Input = int.Parse(Console.ReadLine());Shape s1 = null;while (Input != 4){switch (Input){case 1:Console.WriteLine("Enter The Position: ");Console.Write("x: ");float x = float.Parse(Console.ReadLine()); Console.Write("Y: ");float y = float.Parse(Console.ReadLine()); Point p1 = new Point(x, y);Console.WriteLine("Enter Color: "); int color = int.Parse(Console.ReadLine());Console.WriteLine("Enter Radius: "); int radius = int.Parse(Console.ReadLine());Console.WriteLine("Enter BW: "); float bw = float.Parse(Console.ReadLine());Circle c1 = new Circle(p1, color, bw, radius);s1 = new Circle(p1, color, bw, radius);break;default:break;}}//Shape to arrayShape[] myArr = ShapeIntoArray(s1);} או static void Main(string[] args){Console.WriteLine("Choose Shape: (1: Circle, 2: Rectangle, 3: Triangle, 4: exit)");int Input = int.Parse(Console.ReadLine());while (Input != 4){switch (Input){case 1:Console.WriteLine("Enter The Position: ");Console.Write("x: ");float x = float.Parse(Console.ReadLine()); Console.Write("Y: ");float y = float.Parse(Console.ReadLine()); Point p1 = new Point(x, y);Console.WriteLine("Enter Color: "); int color = int.Parse(Console.ReadLine());Console.WriteLine("Enter Radius: "); int radius = int.Parse(Console.ReadLine());Console.WriteLine("Enter BW: "); float bw = float.Parse(Console.ReadLine());Circle c1 = new Circle(p1, color, bw, radius);Shape s1 = new Circle(p1, color, bw, radius);//Shape to array Shape[] myArr = ShapeIntoArray(s1);break;default:break;}}} אגב אתה נכנס ללואה אינסופית בwhile
פורסם 2011 ביולי 2714 שנים מחבר כן בסופו של דבר גיליתי את הבעיה שלי ואגב החלפתי את הסוויטצ' ב-IF אבל עדיין יש לי בעיה, אני מכניס את ה-SHAPE למערך (בעזרת המתודה) אבל כשאני רוצה להחזיר את המערך (מופיע ב-RETURN של המתודה) הוא כאילו לא מזהה את המערך, וגם אני רוצה להדפיס את הצורה בסוף עלמנת לבדוק וגם פה יש לי בעיותusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Shape{ public class Point { private float x; private float y; // Definition of c'tors public Point(float a, float b) { this.x = a; this.y = b; } public Point() { this.x = 0; this.y = 0; } public Point(Point p) { this.x = p.x; this.y = p.y; } // Definition of the methods public void Setx(float x) { this.x = x; } public void Sety(float y) { this.y = y; } public void Setpoint(Point p) { this.x = p.x; this.y = p.y; } public float Getx() { return this.x; } public float Gety() { return this.y; } public string ToString() { string result = "X= " + this.x + " Y= " + this.y; return result; } public void Move(float dx, float dy) { this.x += dx; this.y += dy; } } public class Shape { protected Point position; protected int color; protected float boardWidth; // Definition of c'tors public Shape() { this.position = new Point(); this.color = 1; this.boardWidth = 0; } public Shape(Point pos, int col, float bw) { this.position = pos; //option 1 this.position = new Point(pos); //option 2 this.color = col; this.boardWidth = bw; } public Shape(Point pos) { this.position = new Point(pos); this.color = 1; this.boardWidth = 0; } // Definition of the methods public string ToString() { string result = this.position.ToString(); result += " Color: " + this.color + " Board-width: " + this.boardWidth; return result; } public void Move(float dx, float dy) { //dx += this.position.Getx(); //dy += this.position.Gety(); //this.position.Setx(dx); //this.position.Sety(dy); //option 2 this.position.Move(dx, dy); } } public class Rectangle : Shape { private int width; private int height; // Definition of c'tors public Rectangle(Point pos, int col, float bw, int width, int height) : base(pos, col, bw) { this.width = width; this.height = height; } public Rectangle(Point pos, int width, int height) : base(pos) { this.width = width; this.height = height; } public string ToString() { return "Rectangle info:" + this.position.ToString() + this.width + " " + this.height; } } public class Circle : Shape { private int radius; // Definition of c'tors public Circle(Point pos, int col, float bw, int radius) : base(pos, col, bw) { this.radius = radius; } public Circle(Point pos, int radius) : base(pos) { this.radius = radius; } public string ToString() { return base.ToString() + " circle info:" + this.radius; } } public class Triangle : Shape { private Point p2; private Point p3; // Definition of c'tors public Triangle(Point pos, int col, float bw, Point p2, Point p3) : base(pos, col, bw) { this.p2 = new Point(p2); this.p3 = new Point(p3); } public Triangle(Point pos, Point p2, Point p3) : base(pos) { this.p2 = new Point(p2); this.p3 = new Point(p3); } } class Program { public static Shape[] ShapeIntoArray(Shape a) { Shape[] ShapeArray = new Shape[0]; for (int i = 0; i < 1; i++) { ShapeArray[i] = a; } return ShapeArray; } static void Main(string[] args) { Console.WriteLine("1: Circle"); Console.WriteLine("2: Rectangle"); Console.WriteLine("3: Triangle"); Console.WriteLine("4: Exit"); Console.Write("Choose Shape: "); int Input = int.Parse(Console.ReadLine()); while (Input != 4) { if (Input == 1) { Console.WriteLine("Enter The Position: "); Console.Write("x: "); float x = float.Parse(Console.ReadLine()); Console.Write("Y: "); float y = float.Parse(Console.ReadLine()); Point p1 = new Point(x, y); Console.WriteLine("Enter Color: "); int color = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Radius: "); int radius = int.Parse(Console.ReadLine()); Console.WriteLine("Enter BW: "); float bw = float.Parse(Console.ReadLine()); Circle c1 = new Circle(p1, color, bw, radius); //Shape s1 = new Circle(p1, color, bw, radius); ShapeIntoArray(c1); } else if (Input == 2) { Console.WriteLine("Enter The Position: "); Console.Write("x: "); float x = float.Parse(Console.ReadLine()); Console.Write("Y: "); float y = float.Parse(Console.ReadLine()); Point p1 = new Point(x, y); Console.WriteLine("Enter Color: "); int color = int.Parse(Console.ReadLine()); Console.WriteLine("Enter BW: "); int bw = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Width: "); int width = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Height: "); int height = int.Parse(Console.ReadLine()); Rectangle r1 = new Rectangle(p1, color, bw, width, height); ShapeIntoArray(r1); } } Console.WriteLine(ShapeArray); Console.ReadLine(); } }}
פורסם 2011 ביולי 2714 שנים אני לא מבין מה אתה מנסה לעשות שם.למה הגדרת מערכך בגודל 0?ולמה הלולאה שלך רצה מ- 0 עד 1?חוץ מזה, איפה בקטע הזה בדיוק אתה מבצע הוספה למערך? אתה כל פעם יוצר מערך חדש בקריאה לפונקציה ומנסה להכניס אליו צורה אחת. איפה נשמרות כל שאר הצורות הקודמות?...או שתגדיר את המערך כמשתנה של המחלקה PROGRAM (ותעשה אותו סטטי), ואז הפונקציות יכירו אותו ולא תצטרך ליצור מערך חדש, או שתגדיר את המערך ב MAIN וכל פעם תשלח אותו לפונקציה.אל תשכח שאתה תצטרך גם להגדיר את המערך לאחר כל הכנסה. יותר קל לעבוד עם המחלקה LIST מאשר עם מערך.ובמקום להגיד שמשהו לא מצליח או לא עובד, תגיד מה השגיאת קומפילציה (במידה ויש)
פורסם 2011 ביולי 2714 שנים מחבר המערל בגודל 0 היה טעות, שמתי מערך בגודל 2 לבדוק את 2 הצורות שאני אשלח אליובעניין הוספה למערך אני רושם את ShapeIntoArray(c1); כי הרי c1 זו הצורה שלי אז אותה אני שולח למתודה שמקבלת משתנה a מסוג Shape ומכניסה אותו למערךהשגיאת קומפילציה שאני מקבל כשאני רוצה להדפיס את המערך בסוף היא The name 'ShapeArray' does not exist in the current context
פורסם 2011 ביולי 2714 שנים זו בדיוק הסיבה שאמרתי לך.המערך שהגדרת מוכר אך ורק בתוך הפונקציה, שכן, שם הגדרת אותו.קרא שוב את ההסבר הקודם שלי על פתרונות אפשריים.
פורסם 2011 ביולי 2714 שנים מחבר טוב נראה לי אני אגדיר מערך ב-MAIN ואשלח אותו למתודה ויחזיר אותו אחרי כל הכנסה של צורה למערך, רק מה אני לא מסתדר עם החתימה של המתודה שמקבלת ומחזירה מערך, אשמח לעזרה.
פורסם 2011 ביולי 2714 שנים בכל מקרה, הדרך היותר פשוטה היא להגדיר את המערך כאובייקט של המחלקה, ולא בתוך ה MAIN, ואז לכל הפונקציות שאתה כותב במחלקה PROGRAM תהיה גישה למערך, ולא תצטרך לשלוח אותו כל פעם לפונקציה.זה לא כזה עניין משתנים ופונקציות סטטיות. לצורך המימוש שלך, פשוט תוסיף את הקידומת STATIC לפני הגדרת המערך והפונקציה.בשביל להרחיב את הידע בנושא, חפש מידע על משתנים ופונקציות סטטיות (אגב יש גם מחלקות סטטיות אבל אין לך צורך בזה).עכשיו אני גם שם לב, שלא עשית שום דבר עם המערך שמוחזר מהפונקציה... נראה לי שיש לך קצת חומר להשלים...
פורסם 2011 ביולי 2714 שנים מחבר יצרתי אובייקט שהוא מערך ששייך למחלקה מסויימת (ARRAY)האם הכי טוב זה להגדיר שהמתודה תקבל את הצורה ותכניס למערך ואז תחזיר אותו או משהו אחר אולי ? public class Array { static Shape[] ShapeArray = new Shape[2]; static Shape[] ShapeIntoArray(Shape[] ShapeArray) { for (int i = 0; i < 2; i++) { ShapeArray[i] = a; } return ShapeArray; } }
פורסם 2011 ביולי 2714 שנים מה בדיוק ניסית לחסוך לעצמך בזה שאתה פשוט עוטף את זה בעוד מחלקה?אני מציע לך 2 פתרונות, ואתה מעדיף לנסות להתפתל עם זה בדרך אחרת, אני לא מבין למה.אני לא נוהג לעשות את זה, אבל שכתבתי לך את הקוד כדי שתבין מה הייתה הכוונה.אני עשיתי שימוש ברשימה במקום במערך, אתה יכול לעשות את זה עם מערך, רק שתצטרך כל פעם לדאוג להגדיל אותו לאחר כל הוספה של צורה.אגב יש לך שם לולאה אין סופית ואני לא מצליח להפסיק להכניס צורות, תבדוק למה.שים לב ששיניתי בסוף גם את ההדפסה, אתה לא יכול סתם ככה להדפיס את המערך ולצפות ש WRITELINE ידע להדפיס את כל האובייקטים שיש בו, אתה צריך לעשות עם לולאה.שים לב, נגעתי רק במחלקה PROGRAM1. הפתרון שהצעתי יכול בהחלט לעבוד, או הוא ממש לא יפתור לך את הבעיה שהייתה לך מקודם, יש לך בדיוק את אותה בעיה רק עטפת אותה במחלקה אחרת.2. גם אם תתקן את זה ותמשיך אם הפתרון שלך, אני לא מבין מה הטעם כל פעם להחזיר את המערך אם אתה לא עושה כלום עם ההפניה שמוחזרת לך מהפונקציה.3. למה בדוגמא שהבאת אתה מכניס את הצורה לכל המקומות במערך? זה ידרוס לך את הצורה הקודמת שהכנסת?4. בדוגמא שלך אין לך סיבה להגדיר את הפונקציה סטטית.5. הפונקציה אמורה לקבל צורה כמו שאמרת ולא מערך של צורות.בכל מקרה, אני ממליץ לך ללמוד קצת את הנושא של משתני מחלקה / משתנים לוקאלים / משתנים סטטיים. לידיעתך, לפעמים נסיון לבצע קומפילציה ובדיקה של השגיאות, יתנו לך גם מושג מה גורם לבעיה, ומשם לנסות להבין למה הדרך לא נכונה.אה כן, והנה הקוד:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Shape{ public class Point { private float x; private float y; // Definition of c'tors public Point(float a, float b) { this.x = a; this.y = b; } public Point() { this.x = 0; this.y = 0; } public Point(Point p) { this.x = p.x; this.y = p.y; } // Definition of the methods public void Setx(float x) { this.x = x; } public void Sety(float y) { this.y = y; } public void Setpoint(Point p) { this.x = p.x; this.y = p.y; } public float Getx() { return this.x; } public float Gety() { return this.y; } public string ToString() { string result = "X= " + this.x + " Y= " + this.y; return result; } public void Move(float dx, float dy) { this.x += dx; this.y += dy; } } public class Shape { protected Point position; protected int color; protected float boardWidth; // Definition of c'tors public Shape() { this.position = new Point(); this.color = 1; this.boardWidth = 0; } public Shape(Point pos, int col, float bw) { this.position = pos; //option 1 this.position = new Point(pos); //option 2 this.color = col; this.boardWidth = bw; } public Shape(Point pos) { this.position = new Point(pos); this.color = 1; this.boardWidth = 0; } // Definition of the methods public string ToString() { string result = this.position.ToString(); result += " Color: " + this.color + " Board-width: " + this.boardWidth; return result; } public void Move(float dx, float dy) { //dx += this.position.Getx(); //dy += this.position.Gety(); //this.position.Setx(dx); //this.position.Sety(dy); //option 2 this.position.Move(dx, dy); } } public class Rectangle : Shape { private int width; private int height; // Definition of c'tors public Rectangle(Point pos, int col, float bw, int width, int height) : base(pos, col, bw) { this.width = width; this.height = height; } public Rectangle(Point pos, int width, int height) : base(pos) { this.width = width; this.height = height; } public string ToString() { return "Rectangle info:" + this.position.ToString() + this.width + " " + this.height; } } public class Circle : Shape { private int radius; // Definition of c'tors public Circle(Point pos, int col, float bw, int radius) : base(pos, col, bw) { this.radius = radius; } public Circle(Point pos, int radius) : base(pos) { this.radius = radius; } public string ToString() { return base.ToString() + " circle info:" + this.radius; } } public class Triangle : Shape { private Point p2; private Point p3; // Definition of c'tors public Triangle(Point pos, int col, float bw, Point p2, Point p3) : base(pos, col, bw) { this.p2 = new Point(p2); this.p3 = new Point(p3); } public Triangle(Point pos, Point p2, Point p3) : base(pos) { this.p2 = new Point(p2); this.p3 = new Point(p3); } } class Program { static List<Shape> ShapesList; public static void ShapeIntoArray(Shape a) { if (ShapesList==null) ShapesList=new List<Shape>(); ShapesList.Add(a); } static void Main(string[] args) { Console.WriteLine("1: Circle"); Console.WriteLine("2: Rectangle"); Console.WriteLine("3: Triangle"); Console.WriteLine("4: Exit"); Console.Write("Choose Shape: "); int Input = int.Parse(Console.ReadLine()); while (Input != 4) { if (Input == 1) { Console.WriteLine("Enter The Position: "); Console.Write("x: "); float x = float.Parse(Console.ReadLine()); Console.Write("Y: "); float y = float.Parse(Console.ReadLine()); Point p1 = new Point(x, y); Console.WriteLine("Enter Color: "); int color = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Radius: "); int radius = int.Parse(Console.ReadLine()); Console.WriteLine("Enter BW: "); float bw = float.Parse(Console.ReadLine()); Circle c1 = new Circle(p1, color, bw, radius); //Shape s1 = new Circle(p1, color, bw, radius); ShapeIntoArray(c1); } else if (Input == 2) { Console.WriteLine("Enter The Position: "); Console.Write("x: "); float x = float.Parse(Console.ReadLine()); Console.Write("Y: "); float y = float.Parse(Console.ReadLine()); Point p1 = new Point(x, y); Console.WriteLine("Enter Color: "); int color = int.Parse(Console.ReadLine()); Console.WriteLine("Enter BW: "); int bw = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Width: "); int width = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Height: "); int height = int.Parse(Console.ReadLine()); Rectangle r1 = new Rectangle(p1, color, bw, width, height); ShapeIntoArray(r1); } } for (int i=0; i<ShapesList.Count; i++) Console.WriteLine(ShapesList[i]); Console.ReadLine(); } }}
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.