1

我正在使用教程编写一个简单的应用程序,该应用程序计算银行账户达到目标金额需要多少年。

我正在尝试使用“如果”语句,因此如果此人的初始余额超过其目标金额,则会打印“您的余额大于目标金额”,但是当我将其写入代码时,它总是会打印以上无论用户输入多少。

这是代码:

        double balance, interestRate, targetBalance; //creating three doubles

        Console.WriteLine("What is your current balance?"); //writing to the console
        balance = Convert.ToDouble(Console.ReadLine()); //reading what the user inputs & converting it to a double

        Console.WriteLine("What is your current annual interest (in %)?");
        interestRate = 1 + Convert.ToDouble(Console.ReadLine()); //same as above

        Console.WriteLine("What balanec would you like to have?");
        targetBalance = Convert.ToDouble(Console.ReadLine()); //same as above

        int totalYears = 0; //creates an int variable for the total years

        do
        {
            balance *= interestRate; //multiplying balance x interest rate
            ++totalYears; // adding 1 to the total years
        }
        while (balance < targetBalance); //only do the above when balance is less than target balance


        if (balance < targetBalance)
        {
            Console.WriteLine("In {0} year{1} you'll have a balance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance); //writing the results to the console
        }
        else if (targetBalance < balance)
        {
            Console.WriteLine("Your balance is bigger than the target amount");
        }
        Console.ReadKey(); //leaving the results there until the user inputs a key
4

3 回答 3

6

do-while退出循环的唯一方法是余额是>=目标余额。因此,您的第一个if语句将永远不会评估为true.

您可能想在进入循环targetBalance < balance之前进行检查。do-while如果余额大于目标,则重新开始。然后在循环之后,无需围绕“在 x 年内...”对话框进行 if 检查。

于 2012-11-10T00:21:24.737 回答
1

它在这里工作正常。但请先检查您的余额,targetBalance。您没有注意到如果它们相等会发生什么。

于 2012-11-10T00:30:46.713 回答
0

我想你想要这个...

double balance, interestRate, targetBalance; //creating three doubles

Console.WriteLine("What is your current balance?"); //writing to the console
balance = Convert.ToDouble(Console.ReadLine()); //reading what the user inputs & converting it to a double

Console.WriteLine("What is your current annual interest (in %)?");
interestRate = 1 + Convert.ToDouble(Console.ReadLine()); //same as above

Console.WriteLine("What balanec would you like to have?");
targetBalance = Convert.ToDouble(Console.ReadLine()); //same as above

int totalYears = 0; //creates an int variable for the total years
if (balance < targetBalance)
{
    do
    {
        balance *= interestRate; //multiplying balance x interest rate
        ++totalYears; // adding 1 to the total years
    }
    while (balance < targetBalance); //only do the above when balance is less than target balance
        Console.WriteLine("In {0} year{1} you'll have a balance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance); //writing the results to the console
    }
 }
 else if (targetBalance < balance)
 {
     Console.WriteLine("Your balance is bigger than the target amount");
 }
 Console.ReadKey(); //leaving the results there until the user inputs a key
于 2012-11-10T21:04:13.823 回答