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:
- Cần một tài liệu tổng hợp các class xây dựng sẵn trong c# kèm theo hướng dẫn sử dụng
- Hỏi cách download code tại website http://www.codeforge.com
- cần giúp đỡ về ẩn hiện các control trong toolbox
- Chọn đề tài thực tập và đồ án tốt nghiệp
- Active link trong asp.net MVC3
- Tìm hiểu về ArrayList
- Telerik Extensions for ASP.NET MVC Open Source có phải là tool free?
- Giúp mình đưa dữ liệu từ 2 table trong SQL sang dataGridView
- [Help] giúp mình cái bài tập C#*(demo 3 lớp}
- bài tập ngày tháng năm
-
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...