צריך עזרה ב-C# בתרגיל - עמוד 2 - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

צריך עזרה ב-C# בתרגיל


moskitos

Recommended Posts

יש לי בא בקוד שלי שהוא לא מדפיס נכון את הנתונים שאני נותן לו ואני לא מוצא איפה הבעיה (ב-2 השורות האחרונות של הקוד אני עושה את הבדיקה)

אשמח אם מישהו יוכל להגיד לי איפה טעיתי, אני מכניס נתונים לצורה אחת ומקבל את הפלט Rectangle Info: 3,3,45 6, חסרים לי 2 הנק' של הצורה ו-2 מספרים מודפסים ביחד (45)

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


namespace ConsoleApplication1
{
public class Point
{
private float x;
private float y;


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;
}


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 void Move(float dx, float dy)
{
this.x += dx;
this.y += dy;
}
}


public class Shape
{
protected Point Position;
protected int Color; //1: black, 2: red, 3: blue, 4: yellow
protected float BorderWidth;


public Shape(Point Position, int Color, float BorderWidth)
{
this.Position = new Point(Position);
this.Color = Color;
this.BorderWidth = BorderWidth;
}


public Shape(Point Position)
{
this.Position = Position;
this.Color = 1;
this.BorderWidth = 1;
}


public virtual string ToString()
{
return Position.GetX() + Position.GetY() + "," + this.Color + "," + this.BorderWidth;
}


public void Move(float dx, float dy)
{
float TempX = this.Position.GetX() + dx;
this.Position.SetX(TempX);


float TempY = this.Position.GetX() + dy;
this.Position.SetX(TempY);
}
}


public class Rectangle : Shape
{
private int Width;
private int Heigth;


public Rectangle(Point Position, int Color, float BorderWidth, int Width, int Heigth)
: base(Position, Color, BorderWidth)
{
this.Width = Width;
this.Heigth = Heigth;
}


public Rectangle(Point Position, int Width, int Heigth)
: base(Position)
{
this.Width = Width;
this.Heigth = Heigth;
}


public override string ToString()
{
return "Rectangle Info: " + base.ToString() + this.Width + " " + this.Heigth;
}
}


public class Circle : Shape
{
private float Radius;


public Circle(Point Position, int Color, float BorderWidth, int Radius)
: base(Position, Color, BorderWidth)
{
this.Radius = Radius;
}


public Circle(Point Position, int Radius)
: base(Position)
{
this.Radius = Radius;
}


public string ToString()
{
return "Circle Info : " + this.Position.ToString() + this.Radius;
}
}


public class Triangle : Shape
{
private Point p2;
private Point p3;


public Triangle(Point Position, Point p2, Point p3, int Color, float BorderWidth)
: base(Position, Color, BorderWidth)
{
this.p2 = p2;
this.p3 = p3;
}


public Triangle(Point Position, Point p2, Point p3)
: base(Position)
{
this.p2 = p2;
this.p3 = p3;
}
}




class Program
{
static void Main(string[] args)
{
//int count = 0;

////define shapes array
//Shape[] ShapesArray = new Shape[20];


//Console.WriteLine("Please Choose Shape:\n" + "--------------------\n" + "1: Rectangle\n" + "2: Triangle\n" + "3: Circle");
//int ShapeInput = int.Parse(Console.ReadLine());


//for (int i = 0; i < ShapesArray.Length; i++)
//{
// switch (ShapeInput)
// {
// case 1:
// //Rectangle
// Console.Write("Enter Position:\n");


// //choose position
// Point Rectangle_Point = new Point(float.Parse(Console.ReadLine()), float.Parse(Console.ReadLine()));


// //choose clolor
// Console.Write("Enter Color: ");
// int Rectangle_Color = int.Parse(Console.ReadLine());


// //choose BorderWidth
// Console.Write("Enter BorderWidth: ");
// float Rectangle_BorderWidth = float.Parse(Console.ReadLine());


// //choose rectangle width
// Console.Write("Enter Width: ");
// int Rectangle_Width = int.Parse(Console.ReadLine());


// //choose rectangle height
// Console.Write("Enter Height: ");
// int Rectangle_Height = int.Parse(Console.ReadLine());


// //create a new rectangle object
// Rectangle Rectangle1 = new Rectangle(Rectangle_Point, Rectangle_Color, Rectangle_BorderWidth, Rectangle_Width, Rectangle_Height);

// //put the shape into array
// ShapesArray[i] = Rectangle1;
// count++;
// break;


// case 2:
// //Triangle
// Console.WriteLine("Enter Position No1:\n" + "X: \n" + "Y: \n");


// //read position from user
// Point TrianglePoint1 = new Point(float.Parse(Console.ReadLine()), float.Parse(Console.ReadLine()));


// Console.WriteLine("Enter Position No2:\n" + "X: \n" + "Y: \n");


// //read position from user
// Point TrianglePoint2 = new Point(float.Parse(Console.ReadLine()), float.Parse(Console.ReadLine()));


// Console.WriteLine("Enter Position No3:\n" + "X: \n" + "Y: \n");


// //read position from user
// Point TrianglePoint3 = new Point(float.Parse(Console.ReadLine()), float.Parse(Console.ReadLine()));


// //create a new rectangle object
// Triangle Tri1 = new Triangle(TrianglePoint1, TrianglePoint2, TrianglePoint3, int.Parse(Console.ReadLine()), float.Parse(Console.ReadLine()));


// //put the shape into array
// ShapesArray[i] = Tri1;
// count++;
// break;


// case 3:
// //Circle
// Console.WriteLine("Enter Position:\n" + "X: \n" + "Y: \n");


// //read position from user
// Point pCircle = new Point(float.Parse(Console.ReadLine()), float.Parse(Console.ReadLine()));


// //create a new rectangle object
// Circle Cr1 = new Circle(pCircle, int.Parse(Console.ReadLine()), float.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()));


// //put the shape into array
// ShapesArray[i] = Cr1;
// count++;
// break;


// case 4:
// //exit
// continue;


// default:
// //choosing of a non-existing number.
// Console.WriteLine("No Such Shape, Try Again Or Press 4 For Exit.");


// break;
// }


// Console.Write("Choose Another Shape Or 4 To Exit: ");
// ShapeInput = int.Parse(Console.ReadLine());


//}


////pribnt the shpes array
//for (int i = 0; i < count; i++)
//{
// Console.WriteLine("Shape #" + (i+1) + ShapesArray[i].ToString());
//}


Point test = new Point(1, 2);
Rectangle Rec = new Rectangle(test, 3, 4, 5, 6);
Console.WriteLine(Rec.ToString());




Console.ReadLine();
}
}
}

קישור לתוכן
שתף באתרים אחרים

לא מבין למה ציפית, אתה פשוט מדפיס 4 ו- 5 ללא פסיק ביניהם אז אתה רואה 45.

שים לב לשורה הבאה שלך:

base.ToString() + this.Width

כמו כן, זה לא מדפיס לך 1 ו- 2, כי ה TOSTRING שלך מחזיר בין השאר

 return Position.GetX() + Position.GetY()

שזה עושה 1+2 שזה 3, ולכן מדפיס לך 3 במקום 1 ואחריו 2.

תשרש להם מחרוזת רווח כדי שזה לא יבצע חיבור אריטמתי.

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

אגב אתה יודע איך לעשות DEBUG ב VISUAL STUDIO?

קישור לתוכן
שתף באתרים אחרים

ארכיון

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

×
  • צור חדש...