无法弄清楚为什么我的If else / Else if语句不起作用。 C#

时间:2015-12-11 11:55:08

标签: c#

试图找出为什么我的其他if / if else语句没有正确执行。 每次我运行它都会跳过if语句,然后转到else,并跳过else语句。它还没有完成,试图让我的第一个类别继续工作。但它是可执行的。

这是给我带来麻烦的部分:

//tells user if they overspent, underspent, or broke even
        //Budget 1
        if (moneyBudget[0]>moneySpent1) {
            Console.WriteLine ("You overspent by $" + moneySpent1 + " this     week.");
        } else if (moneyBudget [0]<moneySpent1) {
            Console.WriteLine ("You underspent by $" + moneySpent1 + " this week.");
        } 
        else {
            Console.WriteLine ("You broke even this week.");
        }

`

要在此处运行,请输入完整代码:

using System;

namespace Conditionals_Assignment
{
class MainClass
{
    public static void Main (string[] args)
    {
        /* 
         */
        //Explains to the user what this program is for and prompts for 3 categories
        Console.WriteLine ("Welcome to the Weekly Budget Calculator!\r\nThis calculator is designed to tell you if you overspent, underspent, or broke even \r\nfor your weekly budget in 3 different categories of spending. Please enter values without the $ sign.");
        Console.WriteLine ("Please enter the name of the first bill you want to budget for this week and press enter.");
        string budgetOneString = Console.ReadLine ();
        //make sure something is entered ant not left blank
        Console.WriteLine (string.IsNullOrWhiteSpace(budgetOneString));
        if (string.IsNullOrWhiteSpace(budgetOneString)) {
            Console.WriteLine ("Please enter a value for category 1");
            //re-ask
            budgetOneString = Console.ReadLine();
        }
        //ask for name of second category budget
        Console.WriteLine ("Please enter the name of the second bill you want to budget for this week and press enter.");
        string budgetTwoString = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrWhiteSpace(budgetTwoString));
        if (string.IsNullOrWhiteSpace(budgetTwoString)) {
            Console.WriteLine ("Please enter a value for category 2");
        budgetTwoString = Console.ReadLine ();
        }
        //asking for 3rd category budget
        Console.WriteLine ("Please enter the name of the third bill you want to budget for this week and press enter.");
        string budgetThreeString = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrWhiteSpace(budgetThreeString));
        if (string.IsNullOrWhiteSpace(budgetThreeString)) {
            Console.WriteLine ("Please enter a value for category 3");
        budgetThreeString = Console.ReadLine();
        }

        //set array for budget categories
        string [] budgetCat = new string[3];

        //fill array with 3 categories set by user
        budgetCat [0] = budgetOneString;
        budgetCat [1] = budgetTwoString;
        budgetCat [2] = budgetThreeString;

        //tells user what they input for categories
        Console.WriteLine ("You entered "+budgetCat [0]+", "+budgetCat[1]+", and "+budgetCat[2]+" for the three categories you would like to budget.");

        //Prompt user to input value for first named category
        Console.WriteLine("How much money did you budget for "+budgetCat [0]+"?");
        string moneyBudgetString1 = Console.ReadLine();
        Console.WriteLine (string.IsNullOrWhiteSpace(moneyBudgetString1));
        if (string.IsNullOrWhiteSpace(moneyBudgetString1)) {
            Console.WriteLine ("Please enter a value.");
            moneyBudgetString1 = Console.ReadLine ();
        }

        decimal money1 = decimal.Parse (moneyBudgetString1);
        Console.WriteLine ("How much money did you budget for " +budgetCat [1]+"?");
        string moneyBudgetString2 = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrWhiteSpace(moneyBudgetString2));
        if (string.IsNullOrWhiteSpace(moneyBudgetString2)) {
            Console.WriteLine ("Please enter a value.");
            moneyBudgetString2 = Console.ReadLine ();
        }
        decimal money2 = decimal.Parse (moneyBudgetString2);
        Console.WriteLine ("Mow much money did you budget for " + budgetCat [2] + "?");
        string moneyBudgetString3 = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrEmpty(moneyBudgetString3));
        if (string.IsNullOrEmpty(moneyBudgetString3)) {
            Console.WriteLine ("Please enter a value.");
            moneyBudgetString3 = Console.ReadLine ();
        }
        decimal money3 = decimal.Parse (moneyBudgetString3);

        //set array with amounts entered
        decimal[] moneyBudget = new decimal[3];
        moneyBudget [0] = money1;
        moneyBudget [1] = money2;
        moneyBudget [2] = money3;

        //tells user what they input for categories
        Console.WriteLine("You entered $"+moneyBudget [0]+" for "+budgetCat [0]+", $"+moneyBudget [1]+" for "+budgetCat [1]+", and $"+moneyBudget [2]+" for "+budgetCat [2]+" for the amount of money your budgeted for each category.");

        //asks user how much they actually spent for eatch category
        Console.WriteLine("How much money did you actually spend for "+budgetCat [0]+"?");
        string moneyActualString1 = Console.ReadLine();
        decimal moneyActual1 = decimal.Parse (moneyActualString1);
        Console.WriteLine ("How much money did you actually spend for "+budgetCat [1]+"?");
        string moneyActualString2 = Console.ReadLine();
        decimal moneyActual2 = decimal.Parse (moneyActualString2);
        Console.WriteLine ("How much money did you actually spend for "+budgetCat [2]+"?");
        string moneyActualString3 = Console.ReadLine();
        decimal moneyActual3 = decimal.Parse (moneyActualString3);

        //set array for amounts entered
        decimal[] actNum = new decimal[3];
        actNum [0] = moneyActual1;
        actNum [1] = moneyActual2;
        actNum [2] = moneyActual3;

        //returns the values entered to the user
        Console.WriteLine("You entered that you actually spent these amounts for each category:\r\n"+budgetCat [0]+" = $"+actNum [0]+"\r\n"+budgetCat [1]+" = $"+actNum[1]+"\r\n"+budgetCat [2]+" = $"+actNum [2]+".");


        //calculates over,under or break even point
        decimal moneySpent1 = moneyBudget [0]-actNum [0];
        decimal moneySpent2 = moneyBudget [1]-actNum [1];
        decimal moneySpent3 = moneyBudget [2]-actNum [2];



        //tells user if they overspent, underspent, or broke even
        //Budget 1
        if (moneyBudget[0]>moneySpent1) {
            Console.WriteLine ("You overspent by $" + moneySpent1 + " this week.");
        } else if (moneyBudget [0]<moneySpent1) {
            Console.WriteLine ("You underspent by $" + moneySpent1 + " this week.");
        } 
        else {
            Console.WriteLine ("You broke even this week.");
        }





    }
}

}

1 个答案:

答案 0 :(得分:1)

添加

Console.WriteLine("Hit any key to continue");
Console.ReadKey();

到Main方法的结尾。您没有等待任何输入,应用程序将在最后一次执行Console.WriteLine后退出。

另外,您可能需要考虑将确定欠/过支出的逻辑更改为。

//tells user if they overspent, underspent, or broke even
//Budget 1
if (moneyBudget[0] < actNum[0])
{
    Console.WriteLine("You overspent by $" + Math.Abs(moneyBudget[0] - actNum[0]) + " this week.");
}
else if (moneyBudget[0] > actNum[0])
{
    Console.WriteLine("You underspent by $" + Math.Abs(moneyBudget[0] - actNum[0]) + " this week.");
}
else
{
    Console.WriteLine("You broke even this week.");
}