【发布时间】:2016-06-24 13:03:14
【问题描述】:
可能是非常简单的事情,哈哈,但是我在试图找出我需要编写的代码以将 GetArea 计算写入文件时遇到了很多麻烦,这里是一些代码,所有帮助都表示赞赏:)
using System;
using System.IO;
namespace system
{
class Rectangle
{
private double length;
private double width;
public void GetDetails()
{
Console.WriteLine("Enter Length: ");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter Width: ");
width = Convert.ToDouble(Console.ReadLine());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea()); // this is what i need to be wrote to the file
}
public void WriteToFile()
{
//this is the method where i need to find out the code to get the the getarea to write to the file
}
}
class CalculateRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.GetDetails();
r.Display();
r.WriteToFile(); //this is the main method in which i need the area of the rectangle to be wrote to the file
Console.ReadLine();
}
}
}
【问题讨论】:
-
我看不到您将对象写入文件的代码。请发帖minimal reproducible example
-
您是否研究过如何将文本写入文件?一个好的谷歌会抛出数千个关于如何做到这一点的结果......
-
System.IO.File.WriteAllText怎么样?你试过了吗?
标签: c# visual-studio-2010 writetofile