Help with simple area/perimeter program for school
Hello,
This is my first post and i need some help. I have to write a program for my java class in college that can take in length and width and display area and perimeter. The program must also say if the shape is a square or rectangle. All this works fine but there is one part not working right. I have to have a if statement that when the length or width is entered as a negative or less then 0 the program prompts the user to use a positive number and then ends. the problem is i can't get the program to end. It needs to finish when ever a negative is entered at either length or width. THIS IS NOT SUPPOSED TO LOOP! so basically how do i stop my program, mid-program right after the first if statement.
Here is the code:
import java.util.Scanner;
class Rectangle1
{
public static void main(String[] args)
{
System.out.println("Rectangle Program");
System.out.println();
Scanner k =new Scanner(System.in);
System.out.print("Type in a width: "); /*<< Receves the width and holds it, If less then zero the program ends*/
double width = k.nextDouble();
if (width <= 0)
{
System.out.println("The width must be greater then 0");
}
System.out.print("Type in a length: "); /*<< Receves the length and holds it, If less then zero the program ends*/
double length = k.nextDouble();
if (length <= 0)
{
System.out.println("The length must be greater then 0");
}
if (width == length) /*<<Determins if the shape is a square or rectangle*/
{
System.out.println("The shape is a square");
}
else
System.out.println("The shape is a rectangle");
double area = width*length;/*<<Formula for determaning area*/
System.out.println("The area of the shape is: "+area);
double perimeter = 2*(width+
length);/*<<Formula for determaning perimeter*/
System.out.println("The perimeter is: "+perimeter);
}
}