none
Output and if statement

    Question

  • Hi everyone,

    I hate having to ask for help, but I guess sometimes you just can not avoid it. Anyways I am not looking for a solution, I am simply looking for a nudge in the right direction. This is homework just so you all know and I am not asking for the answer just for advice.

    Ok so here my code:

    protected void calculateButton_Click( object sender, EventArgs e )
    		{
    			int previousYear = int.Parse(previousTextBox.Text);
    			int currentYear = int.Parse(currentTextBox.Text);
    
    			double entryFee = 25.00;
    
    			double previousYearRevenue = previousYear * entryFee;
    			double currentYearRevenue = currentYear * entryFee;
    			int comparison;
    			double moreRevenue = currentYearRevenue - previousYearRevenue;
    
    			if (currentYear > previousYear)
    			{
    				comparison = currentYear - previousYear;
    			}
    			else
    			{
    				comparison = previousYear - currentYear;
    			}
    
    			resultLabel.Text = String.Format("{0} People Showed Up This Year<br />And Last Year {1} people showed up<br /> The Total Revenues This Year: {2:C}<br /> The Total Revenues Last Year: {3:C}<br />{4} more people showed up In Current Year<br /> And Current Year Made {5:C} more revenue",
    				currentYear,
    				previousYear,
    				currentYearRevenue,
    				previousYearRevenue,
    				comparison,
    				moreRevenue);
    		}
    	}
    }

    So my question is this. And I know I should know this but for some reason it is not clicking. Anyways if you look at the if statement what I would like to do is make a comparison of the two variables  then what I am looking to do is get the result printed to the resultLabel but I would like the currentYear printed in if it had more participants and previous year if it had more people along with the result of how many more people showed up. The revenue part I am doing extra I do know I will need to check that also with an if statement similar to what needs to be done with number of people.

    Thank You

    Woody

    Tuesday, April 4, 2017 3:09 PM

Answers

  • Since you don't want an answer here are a few notes.

    1.

    if (currentYear > previousYear)
    {
    	comparison = currentYear - previousYear;
    }
    else
    {
    	comparison = previousYear - currentYear;
    }

    If you always want a positive value you can do that with the following:

    Math.Abs(currentYear - previousYear);

    2.

    {4} more people showed up In Current Year

    This always assumes the current year ends up with more people.  Try using the if/else statement to set a string variable based on the current or previous year.  Then use that string variable in your results label.


    • Marked as answer by woody876 Tuesday, April 4, 2017 5:56 PM
    Tuesday, April 4, 2017 5:04 PM
  • The Math.Abs always returns a positive results.  Even if the previous year is larger than the current year the Math.Abs will return a positive answer.

    Math.Abs(10 - 15) returns 5.  Even though 10 - 15 is -5.

    I don't think you need to go to the trouble of the resultLabel += route.  It's a valid solution though.  Personally, I would define a string text and then fill it based on your if/else statements.

    • Marked as answer by woody876 Tuesday, April 4, 2017 7:40 PM
    Tuesday, April 4, 2017 6:09 PM

All replies

  • I forgot I am doing this without creating  helper Methods for now.....

    • Edited by woody876 Tuesday, April 4, 2017 3:15 PM
    Tuesday, April 4, 2017 3:10 PM
  • Since you don't want an answer here are a few notes.

    1.

    if (currentYear > previousYear)
    {
    	comparison = currentYear - previousYear;
    }
    else
    {
    	comparison = previousYear - currentYear;
    }

    If you always want a positive value you can do that with the following:

    Math.Abs(currentYear - previousYear);

    2.

    {4} more people showed up In Current Year

    This always assumes the current year ends up with more people.  Try using the if/else statement to set a string variable based on the current or previous year.  Then use that string variable in your results label.


    • Marked as answer by woody876 Tuesday, April 4, 2017 5:56 PM
    Tuesday, April 4, 2017 5:04 PM
  • Thank You for the input and no I don't want the answer that would defeat my purpose of trying to learn the language. I will try the Math.Abs() way one question though what if the previous year ends up being more then the  current year? And I am trying to get the result into the resultLabel for either the previous year had x amount more or if current year had more then current year had x amount more.

    I am thinking I may need to create a couple of different variables or just add a resultLabel += under the math in the if statement....

    Tuesday, April 4, 2017 5:59 PM
  • The Math.Abs always returns a positive results.  Even if the previous year is larger than the current year the Math.Abs will return a positive answer.

    Math.Abs(10 - 15) returns 5.  Even though 10 - 15 is -5.

    I don't think you need to go to the trouble of the resultLabel += route.  It's a valid solution though.  Personally, I would define a string text and then fill it based on your if/else statements.

    • Marked as answer by woody876 Tuesday, April 4, 2017 7:40 PM
    Tuesday, April 4, 2017 6:09 PM
  • Thanks Again Ryan
    Tuesday, April 4, 2017 7:40 PM