2017-02-03 50 views
0

因此,我试图让一个酒店经理使用它自己的类来计算成本,具体取决于房间的高度,海景,多少间卧室和它根据这3个变量吐出成本。c#编写一个管理酒店房间的代码/计算

我被卡住了,因为当我尝试运行这个没有任何计算,只是为了看看它是否工作到目前为止它不会返回/重申我已经输入。有一个错误说“FormatException发生”

我对c#很新,所以我很感激任何可以给予的帮助。

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; } 

} 

} 

回答

5

Console.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); 
+0

谢谢,我接受了你的建议,它的工作很棒。 现在,如果我想添加变量,取决于房间是否有1卧室800 $,2卧室850,3bedrooms 900 $。使用这些数字作为基准价格,然后增加50美元的海景,然后25美元,如果你的公寓数量高于300. 我会使用另一个字符串致力于货币从0 $开始? –

0

你的格式字符串有更多的插值点比你有参数。尝试:

Console.WriteLine("{0} {1} {2} {3} {4}", 
     myApartment.BuildingNumber, 
     myApartment.ApartmentNumber, 
     myApartment.Type, 
     myApartment.View, 
     myApartment.View); 
+0

谢谢,我解决了它,它的工作原理!现在如果我想添加变量,取决于房间是否有1间卧室800 $,2卧室850,3床房900 $。使用这些数字作为基准价格,然后增加50美元的海景,然后25美元,如果你的公寓号码高于300.我会做另一个字符串专用于货币从0 $开始?然后比较他们输入的数字,比如他们为1个卧室添加1,添加800 $? –