简体   繁体   English

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

[英]If statement not working in C#

I'm using a tutorial to write a simple app that calculates how many years it will take for a bank account to reach a target amount. 我正在使用一个教程编写一个简单的应用程序,该应用程序可以计算银行帐户达到目标金额需要多少年。

I'm trying to use an "If" statement, so if the person's beginning balance is more than their target amount, it prints "Your balance is bigger than the target amount", but when I write this into my code, it always prints the above no matter what amounts the user inputs. 我正在尝试使用“ If”语句,因此,如果该人的期初余额超过其目标金额,则会显示“您的余额大于目标金额”,但是当我将此代码写入代码时,它始终会显示无论用户输入多少,以上内容都是如此。

Here's the code: 这是代码:

        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

The only way for your do-while loop to exit is when the balance is >= the target balance. 退出do-while循环的唯一方法是当余额>=目标余额时。 Because of this, your first if statement will never evaluate to true . 因此,您的第一个if语句永远不会评估为true

You probably want to do your targetBalance < balance check before you enter your do-while loop. 在进入do-while循环之前,您可能想要进行targetBalance < balance检查。 If the balance is greater than the target, start over. 如果余额大于目标,请重新开始。 And then after the loop, there is no need to do the if check around your 'In x years...' dialog. 然后,在循环之后,无需对“在x年内...”对话框进行if检查。

It works fine here. 它在这里工作正常。 But please check your balance,targetBalance first. 但是请先检查您的余额,然后再确定目标余额。 You didn't notice that what would happen If they are equal. 您没有注意到,如果它们相等,将会发生什么。

I think you want this... 我想你要这个...

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