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:
- Vấn đề về đối số cho hàm Main()
- Cần các bác pro cho ý tưởng: Lấy dữ liệu tự động từ site khác trong ASP.NET MVC4
- Giúp đỡ convert một dãy số sang kiểu datetime
- Add/Delete Node trên TreeView
- Các bạn cho mình hỏi về chạy thông báo bằng datagridview
- Cần giúp về truyền mảng vào sự kiện click trong asp.net C#
- [sharpGL] mình chạy chương trình mẫu nhưng cứ báo lỗi này suốt!
- Lỗi tạo UserControl trong c#
- Ảnh bitmap nhị phân
- in hóa đơn bán hàng cho một khách hàng trong c#
-
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 ++.
Pallet nhựa Lâm Đồng giải pháp hoàn hảo cho nhà kho Pallet nhựa Lâm Đồng là một giải pháp đa năng và hiệu quả trong việc vận chuyển và lưu trữ hàng hóa. Trong đó, pallet nhựa Lâm Đồng không chỉ đáp...
Pallet nhựa Lâm Đồng giải pháp...