【发布时间】:2019-03-25 20:55:21
【问题描述】:
我的方法 CalculateFee 给了我一个错误。只是名称“CalculateFee”下的一条红线
我尝试将公共更改为私人,但没有帮助,甚至无效,即使这对我来说有点愚蠢......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FineForOverdueBooks
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("How many books were checked out?");
int booksCheckedOut = Convert.ToInt32(Console.ReadLine());
{
if (booksCheckedOut == 1)
{
Console.WriteLine("How many days is it overdue?");
}
else
Console.WriteLine("How many days are they overdue?");
}
int daysOverdue = Convert.ToInt32(Console.ReadLine());
int output = CalculateFee(booksCheckedOut, daysOverdue);
Console.WriteLine("Your late fee is: ", output.ToString("C"));
}
public static int **CalculateFee**(int booksCheckedOut, int daysOverdue)
{
double libraryFine = .10;
double additionalFine = .20;
if (daysOverdue <= 7)
{
for (double x = 0; x <= 7; x++)
{
libraryFine += .10;
}
return Convert.ToInt32(libraryFine);
}
if (daysOverdue > 7)
{
for (int x =0; x<=daysOverdue; x++)
{
additionalFine += .20;
}
return Convert.ToInt32(additionalFine);
}
}
}
}
我只是希望我的方法能够执行,以便查看我的 for 循环是否正确以及是否需要更改一些内容(我可能会这样做)
抱歉,如果这看起来是重复的,我找不到与我的问题直接相关的任何内容。
【问题讨论】:
-
你得到什么错误?
-
并非所有代码路径都返回值
-
在@ZoharPeled类里面,只是缩进错了。
-
您有错误,因为您有 2 个 if。因此,您可以将第二个 if 替换为 else 或在方法的末尾放置另一个 return,以便所有路径都返回一个值。
-
@nbokmans 是的,缩进让我很困惑。一旦意识到这一点,我就删除了我的评论。