為什么面積無法顯示,想知道哪里錯了
using System;
using System.Collections.Generic;
using System.Linq;using System.Text;
using System.Threading.Tasks;
namespace 介面的實作
{ interface IShape
{ int Length{get;set;}
int Width{get;set;}
void GetArea(); }
class Test:IShape
{
public int getarea()
{ return Length * Width; }
public int chang{get;set;}
public int kuan{ get; set;}
public int Length{ get{return Length;} set{Length=value;}
}
public int Width
{
get{return Width;}
set{Width=value;}
}
public void GetArea(){ Console.WriteLine("矩形的面積:{0}",(chang*kuan));}
class Program {
static void Main(string[] args) {
Console.WriteLine("請輸入矩形的寬:"); Console.ReadLine();
Console.WriteLine("請輸入矩形的長:"); Console.ReadLine();
Test getarea = new Test(); Console.WriteLine("矩形的面積:{0}",getArea()); Convert.ToInt32(Console.ReadLine()); } } }}
uj5u.com熱心網友回復:
控制臺輸入的內容,沒有用變數接收uj5u.com熱心網友回復:
能不能說詳細點(新手不太懂)
uj5u.com熱心網友回復:
控制臺輸入的內容,沒有用變數接收Console.WriteLine("請輸入矩形的寬:"); Console.ReadLine();Console.WriteLine("請輸入矩形的長:"); Console.ReadLine(); 這個地方要有變數接受啊
uj5u.com熱心網友回復:
Test getarea = new Test();Console.WriteLine("請輸入矩形的寬:");
var widthStr = Console.ReadLine();
while (!int.TryParse(widthStr, out getarea.Width))
{
Console.WriteLine("輸入的內容不是整數,請輸入矩形的寬:");
widthStr = Console.ReadLine();
}
Console.WriteLine("請輸入矩形的長:");
var lengthStr = Console.ReadLine();
while (!int.TryParse(lengthStr, out getarea.Length))
{
Console.WriteLine("輸入的內容不是整數,請輸入矩形的長:");
lengthStr = Console.ReadLine();
}
Console.WriteLine("矩形的面積:{0}", getarea.getArea());
Console.ReadLine();
uj5u.com熱心網友回復:
最后的getArea報錯,“介面的實作.Test”不包含“getArea”的定義,并且找不到可接受型別為“介面的實作.Test”的第1個引數的擴展方法“getArea”
uj5u.com熱心網友回復:
重復貼子。你的 GetArea 方法需有回傳型別。
interface IShape
{
int Length{get;set;}
int Width{get;set;}
int GetArea(); //不能是一個void 的方法,需要有回傳型別。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/39831.html
標籤:C#
上一篇:大神求助!!!
