繁体   English   中英

如果语句在C#中不起作用

[英]If statement not working in C#

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

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

这是代码:

        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

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

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

它在这里工作正常。 但是请先检查您的余额,然后再确定目标余额。 您没有注意到,如果它们相等,将会发生什么。

我想你要这个...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM