vendredi 20 février 2015

If-Else statments c#

I am having a bit out trouble with my If-Else statements at the end of this code. I am only in week 3 of my programming class and this is my first post here, I have been going to http://ift.tt/1h2aFEI but have been recommended here for some code help. Basically, I have to get the length and width for 2 rectangles. Than determine if they have the same area, of if rectangle 1 is < or > rectangle 2. When I test this code, no matter what values I put in, the response is that the areas are the same when they shouldn't be. Thank you everyone in advance.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace twoRectangles
{
class Program
{


static void Main(string[] args)
{//declare variables and methods
double length = 0.00;
double width = 0.00;
double lengthTwo = 0.00;
double widthTwo = 0.00;
double area1 = length * width;
double area2 = lengthTwo * widthTwo;
getArea1(ref length, ref width);
getArea2(ref lengthTwo, ref widthTwo);
greaterArea(ref area1, ref area2);
}//call method getArea1
static void getArea1(ref double length, ref double width)
{//input for length of first rectangle
Console.WriteLine("Please enter the length of the first rectangle:");
while (!double.TryParse(Console.ReadLine(), out length))
Console.WriteLine("Error, please enter a valid number");
//input for width of frist rectangle
Console.WriteLine("lease enter the width of the first retangle:");
while (!double.TryParse(Console.ReadLine(), out width))
Console.WriteLine("Error, please enter a valid number");
}//call method get Area2
static void getArea2(ref double lengthTwo, ref double widthTwo)
{//input for length of second rectangle
Console.WriteLine("Please enter the length of the second rectangle:");
while (!double.TryParse(Console.ReadLine(), out lengthTwo))
Console.WriteLine("Error, please enter a valid number");
//input for width of second rectangle
Console.WriteLine("Please enter the width of the second rectangle:");
while (!double.TryParse(Console.ReadLine(), out widthTwo))
Console.WriteLine("Error, please enter a valid number");

}//call method greaterArea
static void greaterArea(ref double area1, ref double area2)
{//if statements
if (area1 == area2)
{
Console.WriteLine("The areas of the rectangles are the same");
}
else if(area1 > area2)
{
Console.WriteLine("The area of Rectangle 1 is greater than Rectangle 2");
}
else if(area1 < area2)
{
Console.WriteLine("The area of Rectangle 1 is less than Rectangle 2");
}


}
}

Aucun commentaire:

Enregistrer un commentaire