簡體   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