问题描述
我为 cash.c 编写了代码,但我无法编译它。我的中的每个//最少硬币数量以进行一定数量的更改部分都有一个“预期表达式”错误。我尝试更改它几次,甚至查看我到底做错了什么,但我找不到任何东西。有人可以帮帮我吗?另外,我很想听听关于我的代码的任何建议或反馈!代码在下面。
谢谢! 艾琳娜
public class AddresspageModel : PageModel
{
public SelectList CountryNamesSL { get; set; }
[BindProperty]
public Address Address { get; set; }
public void PopulateCountriesDropDownList(TitanContext _context,object selectedCountry = null)
{
IOrderedQueryable<Country> countriesQuery = from c in _context.Countries
orderby c.displayOrder ascending,c.Name
select c;
if (selectedCountry == null)
selectedCountry = _context.Countries.OrderBy(c => c.displayOrder).OrderBy(c => c.Name).First();
CountryNamesSL = new SelectList(countriesQuery.AsNoTracking(),nameof(Country.CountryID),nameof(Country.Name),selectedCountry);
ViewData["CountryNamesSL"] = CountryNamesSL;
}
public void MapAddress(Address destinationObject)
{
destinationObject.NameOrNumber = Address.NameOrNumber;
destinationObject.Street = Address.Street;
destinationObject.AddressLine2 = Address.AddressLine2;
destinationObject.AddressLine3 = Address.AddressLine3;
destinationObject.City = Address.City;
destinationObject.County = Address.County;
destinationObject.PostCode = Address.PostCode;
destinationObject.Country = Address.Country;
destinationObject.CountryID = Address.CountryID;
}
}
我还想知道我在使用 cs50 时遇到困难是否正常?我理解讲座和短片中的所有内容,但问题集似乎花了我很长时间。我花了大约 3 周的时间来完成 mario.c,如果不使用谷歌搜索我就无法完成。这让我怀疑我是否应该在没有任何经验的情况下听这门课程。我真的很喜欢这门课程,但是您认为我应该将其降低一个档次并从更适合初学者的课程开始吗?
解决方法
您需要在每个“之间”比较中重复 cents
:
// Wrong: while (cents >= .1 && < 0.25)
while (cents >= .1 && cents < 0.25) ...
// Wrong: while (cents >= .05 && < .1) ...
while (cents >= .05 && cents < .1) ...
// Wrong: while (cents >= 0.01 && < .05) ...
while (cents >= 0.01 && cents < .05) ...
,
对于初学者这样的条件
while (cents >= 0.25)
无论如何都没有意义,因为变量 cents
被声明为具有 int
类型。
int cents;
你的意思是
while (cents >= 25 )
在其他 while 循环中,你有一个语法错误,例如在这个 while 循环中
while (cents >= .1 && < 0.25)
你至少需要写
while (cents >= 10 && cents < 25)
但是当控件到达这个 while 循环时,由于前面的 while 循环,变量 cents
已经有一个小于 25
的值。所以写就够了
while ( cents >= 10 )
所以你的循环看起来像
while ( cents >= 25 )
{
coins++;
cents = cents - 25;
}
while ( cents >= 10 )
{
coins++;
cents = cents - 10;
}
while ( cents >= 5 )
{
coins++;
cents = cents - 5;
}
while (cents >= 1 )
{
coins++;
cents = cents - 1;
}