Results 1 to 4 of 4
  1. #1
    icarus is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    how do i stop my program, mid-program right after the first if statement.
    Some ways to exit a method:
    return;
    System.exit(0);
    throw an Exception

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    take in values for width and length then use an if statement using || "or" for both values equating to zero and wrap the if statement arount the whole code.

  4. #4
    icarus is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. School project: network program via java any ideas?
    By Sernomicus in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 04:46 PM
  2. Replies: 4
    Last Post: 11-02-2009, 04:41 PM
  3. Replies: 6
    Last Post: 06-12-2008, 10:14 AM
  4. Replies: 2
    Last Post: 01-07-2008, 08:45 PM
  5. Beginner Needs Help w/ Program for School
    By badness in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •