Chủ đề: Mọi người chỉ e code này với ạ!
-
06-06-2016, 12:11 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Mọi người chỉ e code này với ạ!
Chào mọi người ạ, e có bài tập này mà e viết xong nó báo lỗi và không hiện kết quả. Đề bài là cho nhập vào chiều dài và rộng của hình chữ nhật, xuất ra chi vi và diện tích của nó, yêu cầu phải có các getter và setter, có phương thức khởi tạo cho mỗi biến và cắc phương thức để xuất kết quả. Mọi người chỉ e với, e săp thi mà e k biết gì về OOP cả, e cám ơn rất nhiều ạ.
Code của class
class manageClass
{
private double width, height;
private int xPos, yPos;
//Getter and setter
public double Width
{
get { return width; }
set { width = value; }
}
public double Height
{
get { return height; }
set { height = value; }
}
public int Posx
{
get { return xPos; }
set { xPos = value; }
}
public int Posy
{
get { return yPos; }
set { yPos = value; }
}
public manageClass()
{
Width = double.Parse(Console.ReadLine());
Height = double.Parse(Console.ReadLine());
Posx = int.Parse(Console.ReadLine());
Posy = int.Parse(Console.ReadLine());
}
public double getArea()
{
manageClass a = new manageClass();
double area = a.width * a.height;
return getArea();
}
public double getPerimeter()
{
manageClass a = new manageClass();
double perimeter = 2 * (a.width + a.height);
return getPerimeter();
}
}
class Program
{
static void Main()
{
Console.WriteLine("Ban hay nhap cac thong so can thiet");
manage.manageClass a = new manage.manageClass();
Console.WriteLine("Chieu dai va chieu rong la: {0}, {1}", a.Width, a.Height);
Console.WriteLine("Chu vi va dien tich la: {0}, {1}", a.getArea(), a.getPerimeter());
Console.ReadLine();
}
}
View more random threads:
- Tại sao lại phải ... = New abc(); ?
- moi nguoi giup em bai nay vs
- Form này chứa form kia?
- [Help]Lỗi khi sử dụng thư viện autoit trong C#
- Sửa giúp lỗi Cross-Thread trong bài truyền nhận file qua mạng LAN
- Ai giup em một tay với về phần Image
- giúp đỡ làm bài c
- lọc tin trùng nhau khi lấy tin rss
- Xin các bài tập về lập trình Windown form c# ! Càng nhiều càng tốt! Đang luyện !
- Input Method Editor(IME) - bàn phím tiếng Nhật
-
06-06-2016, 12:47 PM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Mã:class manageClass{ private double width, height; private int xPos, yPos; //Getter and setter public double Width{ get { return width; } set { width = value; } } public double Height{ get { return height; } set { height = value; } } public int Posx{ get { return xPos; } set { xPos = value; } } public int Posy{ get { return yPos; } set { yPos = value; } } //.ctor public manageClass(){ Width = double.Parse(Console.ReadLine()); Height = double.Parse(Console.ReadLine()); Posx = int.Parse(Console.ReadLine()); Posy = int.Parse(Console.ReadLine()); } //methods public double getArea(){ manageClass a = new manageClass(); //double area = a.width * a.height; return a.width * a.height; //getArea(); } public double getPerimeter(){ manageClass a = new manageClass(); //double perimeter = 2 * (a.width + a.height); return 2 * (a.width + a.height); //getPerimeter(); }}
-
06-06-2016, 03:40 PM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
E cám ơn a ạ, e đã sửa nhưng nó vẫn bị lỗi như hình.
-
06-06-2016, 03:52 PM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Width = double.Parse(Console.ReadLine());
//
System.FormatException liên quan đến nhập số liệu.
https://msdn.microsoft.com/en-us/library/system.formatexception(v=vs.110).aspx
-
06-06-2016, 04:16 PM #5Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
A cho e ví dụ được k ạ, e đọc thấy lơ mơ quá ạ
-
06-06-2016, 04:17 PM #6Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Đã test :
Mã:using System;//csc c02.cs class manageClass{ private double width, height; private int xPos, yPos; //Getter and setter public double Width{ get { return width; } set { width = value; } } public double Height{ get { return height; } set { height = value; } } public int Posx{ get { return xPos; } set { xPos = value; } } public int Posy{ get { return yPos; } set { yPos = value; } } //.ctor public manageClass(){ Console.Write("width = "); width = double.Parse(Console.ReadLine()); Console.Write("height = "); height = double.Parse(Console.ReadLine()); //Console.Write("xPos = "); xPos = int.Parse(Console.ReadLine()); //Console.Write("yPos = "); yPos = int.Parse(Console.ReadLine()); } //methods public double getArea(){ //manageClass a = new manageClass(); //double area = a.width * a.height; return width * height; //getArea(); } public double getPerimeter(){ //manageClass a = new manageClass(); //double perimeter = 2 * (a.width + a.height); return 2 * (width + height); //getPerimeter(); } // static void Main(){ Console.WriteLine("Ban hay nhap cac thong so can thiet"); manageClass a = new manageClass(); Console.WriteLine("Chieu dai va chieu rong la: a = {0}, b = {1}", a.Width, a.Height); Console.WriteLine("Chu vi va dien tich la: P = {0}, S = {1}", a.getPerimeter(), a.getArea()); Console.Write(" Done, press enter to quit."); Console.ReadLine(); } }
-
06-06-2016, 04:27 PM #7Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Cuối cùng cũng dc, e cám ơn a ạ.
-
06-06-2016, 05:54 PM #8Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Không cần cám ơn.
//
Cái gì chưa cần, không nên đưa vào (xPos, yPos, Posx, Posy) nó chỉ làm rối - chi phối suy nghĩ.
Xong bước này, mở rộng sang bước khác, version ++.
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...