使用Filestream文件流和StreamWriter对象
创建控制台程序,添加Book类,包含属性书名(string),出版社(string),作者(string),价格(double).创建几个图书对象,将他们的属性值打印到文...
创建控制台程序,添加Book类,包含属性书名(string),出版社(string),作者(string),价格(double).
创建几个图书对象,将他们的属性值打印到文本文件.
提示:
(1)使用Filestream文件流和StreamWriter对象.
(2)多本书,需要循环输入 展开
创建几个图书对象,将他们的属性值打印到文本文件.
提示:
(1)使用Filestream文件流和StreamWriter对象.
(2)多本书,需要循环输入 展开
展开全部
下面就是你要的
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
StreamWriter streamWriter = new StreamWriter(@"D:\Book.txt",true);
Book book = new Book();
while (true)
{
Console.WriteLine("请输入书名:");
book.Name = Console.ReadLine();
Console.WriteLine("请输入出版社:");
book.Publisher = Console.ReadLine();
Console.WriteLine("请输入作者:");
book.Author = Console.ReadLine();
Console.WriteLine("请输入价格:");
book.Cost = Convert.ToDouble(Console.ReadLine());
Console.Clear();
streamWriter.Write("书名:"+book.Name+" ");
streamWriter.Write("出版社:"+book.Publisher+" ");
streamWriter.Write("作者:"+book.Author+" ");
streamWriter.WriteLine("价格:" + book.Cost);
streamWriter.Flush();
}
}
}
class Book
{
public Book()
{
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string publisher;
public string Publisher
{
get { return publisher; }
set { publisher = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
private double cost;
public double Cost
{
get { return cost; }
set { cost = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
StreamWriter streamWriter = new StreamWriter(@"D:\Book.txt",true);
Book book = new Book();
while (true)
{
Console.WriteLine("请输入书名:");
book.Name = Console.ReadLine();
Console.WriteLine("请输入出版社:");
book.Publisher = Console.ReadLine();
Console.WriteLine("请输入作者:");
book.Author = Console.ReadLine();
Console.WriteLine("请输入价格:");
book.Cost = Convert.ToDouble(Console.ReadLine());
Console.Clear();
streamWriter.Write("书名:"+book.Name+" ");
streamWriter.Write("出版社:"+book.Publisher+" ");
streamWriter.Write("作者:"+book.Author+" ");
streamWriter.WriteLine("价格:" + book.Cost);
streamWriter.Flush();
}
}
}
class Book
{
public Book()
{
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string publisher;
public string Publisher
{
get { return publisher; }
set { publisher = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
private double cost;
public double Cost
{
get { return cost; }
set { cost = value; }
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询