我试图将X天的数量加倍。
所以,3天= 4便士,4天= 8便士等
我坚持要把正确的代码放进去:(我知道我已经很近了,我知道我错过了一些愚蠢的事情。我花了5个小时就完成了这件事。我只是想看看解决方案,所以我的想法会把它放在一起如何工作......)
我终于明白了...现在......我怎么能清理它?我还在学习用更少的代码编写代码......但是我现在正在开始使用Visual C#Book ...
// Local variables. /
int daysWorkedInputValue;
decimal currentPayRate, newPayRate, totalPaySalary;
int daysWorked;
int count = 0;
currentPayRate = 0.01m;
totalPaySalary = 0m;
daysWorkedInputValue = int.Parse(daysWorkedInputTextBox.Text);
if (int.TryParse(daysWorkedInputTextBox.Text, out daysWorked))
{
if (daysWorked >= 0)
{
// Continue to process the input. /
if (daysWorkedInputValue == 0)
{
totalPayCalcLabel.Text = "$0.00";
}
if (daysWorkedInputValue == 1)
{
totalPayCalcLabel.Text = "$0.01";
}
// The following loop calculates the total pay. /
while (count <= (daysWorked - 1))
{
// Calculate the total pay amount. /
if (count == 1)
{
currentPayRate = 0.01m;
totalPayCalcLabel.Text = currentPayRate.ToString("c");
}
currentPayRate = currentPayRate * 2;
totalPaySalary = currentPayRate;
if (count >= 1)
{
totalPayCalcLabel.Text = totalPaySalary.ToString("c");
}
// Add one to the loop counter. /
count = count + 1;
// Return focus back to the organisms TextBox. /
daysWorkedInputTextBox.Focus();
}
答案 0 :(得分:5)
我在这里看到一个非常明显的模式:
3 days -> 2^(3-1) = 4 pennies
4 days -> 2^(4-1) = 8 pennies
所以你想解决这个等式:
pennies = 2^(days-1)
我没有运行你的代码来查看错误(没有改变count
或daysWorked
的值,所以我假设你最终会陷入无限循环中),但是这个应该工作得很好:
var pennies = Math.Pow(2, days - 1);