c#编写管理酒店房间/计算的代码
所以我想让一个酒店经理使用它自己的类来计算成本,这取决于你的房间有多高,海景,有多少间卧室,然后根据这三个变量来计算成本 我被卡住了,因为当我尝试在没有任何计算的情况下运行它时,只是为了看看它是否有效,它不会返回/重述我输入的内容。“发生FormatException”时出错 我对c#很陌生,所以我非常感谢能给我的任何帮助c#编写管理酒店房间/计算的代码,c#,C#,所以我想让一个酒店经理使用它自己的类来计算成本,这取决于你的房间有多高,海景,有多少间卧室,然后根据这三个变量来计算成本 我被卡住了,因为当我尝试在没有任何计算的情况下运行它时,只是为了看看它是否有效,它不会返回/重述我输入的内容。“发生FormatException”时出错 我对c#很陌生,所以我非常感谢能给我的任何帮助 using System; using System.Collections.Generic; using System.Linq; using System.Text; u
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace myHotel
{
class Program
{
static void Main(string[] args)
{
Apartment myApartment = new Apartment();
Console.WriteLine("Hotel Building Number:");
myApartment.BuildingNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter your Hotel room number:");
myApartment.ApartmentNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of Bedrooms you have:");
myApartment.Type = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter 1 for Ocean View and 2 for no Ocean view:");
myApartment.View = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter your name");
myApartment.Name = Console.ReadLine();
Console.WriteLine("{0} {1} {2} {3} {4} {5}",
myApartment.BuildingNumber,
myApartment.ApartmentNumber,
myApartment.Type,
myApartment.View,
myApartment.View);
Console.ReadLine();
}
}
class Apartment
{
public int BuildingNumber { get; set; }
public int ApartmentNumber { get; set; }
public int Type { get; set; }
public int View { get; set; }
public string Name { get; set; }
}
}
控制台的格式字符串。WriteLine有6个占位符:
"{0} {1} {2} {3} {4} {5}"
但您只传递了5个参数。请再添加一个参数,或删除最后一个占位符
另外,如果您想显示公寓类实例中的值,那么重写它的
ToString()
方法可能会有意义。例如:
class Apartment
{
public int BuildingNumber { get; set; }
public int ApartmentNumber { get; set; }
public int Type { get; set; }
public int View { get; set; }
public string Name { get; set; }
public override string ToString()
{
return $"{BuildingNumber} {ApartmentNumber} {Type} {View} {Name}";
}
}
现在显示它将非常简单,如下所示:
Console.WriteLine(myApartment);
控制台的格式字符串。WriteLine有6个占位符:
"{0} {1} {2} {3} {4} {5}"
但您只传递了5个参数。请再添加一个参数,或删除最后一个占位符
另外,如果您想显示公寓类实例中的值,那么重写它的
ToString()
方法可能会有意义。例如:
class Apartment
{
public int BuildingNumber { get; set; }
public int ApartmentNumber { get; set; }
public int Type { get; set; }
public int View { get; set; }
public string Name { get; set; }
public override string ToString()
{
return $"{BuildingNumber} {ApartmentNumber} {Type} {View} {Name}";
}
}
现在显示它将非常简单,如下所示:
Console.WriteLine(myApartment);
格式字符串的插值点比参数多。尝试:
Console.WriteLine("{0} {1} {2} {3} {4}",
myApartment.BuildingNumber,
myApartment.ApartmentNumber,
myApartment.Type,
myApartment.View,
myApartment.View);
格式字符串的插值点比参数多。尝试:
Console.WriteLine("{0} {1} {2} {3} {4}",
myApartment.BuildingNumber,
myApartment.ApartmentNumber,
myApartment.Type,
myApartment.View,
myApartment.View);
谢谢,我接受了你的建议,效果很好。现在,如果我想添加变量,取决于是否有一间卧室800美元,两间卧室850美元,三间卧室900美元。使用这些数字作为基准价格,然后海景加50美元,如果你的公寓号码高于300,则加25美元。我是否可以制作另一个字符串,用于从0$开始的货币?谢谢,我采纳了你的建议,效果非常好。现在,如果我想添加变量,取决于是否有一间卧室800美元,两间卧室850美元,三间卧室900美元。使用这些数字作为基准价格,然后海景加50美元,如果你的公寓号码高于300,则加25美元。我是否可以制作另一个字符串,用于从0$开始的货币?谢谢,我修复了它,它工作了!现在,如果我想添加变量,取决于是否有一间卧室800美元,两间卧室850美元,三间卧室900美元。使用这些数字作为基准价格,然后海景加50美元,如果你的公寓号码高于300,则加25美元。我是否可以制作另一个字符串,用于从0$开始的货币?然后比较他们输入的数字,比如如果他们把一间卧室换成一间,加上800美元?谢谢,我修好了,效果很好!现在,如果我想添加变量,取决于是否有一间卧室800美元,两间卧室850美元,三间卧室900美元。使用这些数字作为基准价格,然后海景加50美元,如果你的公寓号码高于300,则加25美元。我是否可以制作另一个字符串,用于从0$开始的货币?然后与他们输入的数字进行比较,比如如果他们把一间卧室换成一间,那么加上800美元?