Results 1 to 4 of 4
- 09-25-2010, 08:53 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 2
- Rep Power
- 0
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);
}
}
- 09-25-2010, 09:01 PM #2
Some ways to exit a method:how do i stop my program, mid-program right after the first if statement.
return;
System.exit(0);
throw an Exception
- 09-26-2010, 01:56 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 09-26-2010, 04:21 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
School project: network program via java any ideas?
By Sernomicus in forum New To JavaReplies: 2Last Post: 12-07-2009, 03:46 PM -
school assignment: creating a simple bank, need help
By runlos in forum New To JavaReplies: 4Last Post: 11-02-2009, 03:41 PM -
To get the points on the perimeter of a Rounded Rectangle
By rashibahal in forum New To JavaReplies: 6Last Post: 06-12-2008, 09:14 AM -
Need help with a program for high school regarding multi-dimensional arrays.
By Torque in forum New To JavaReplies: 2Last Post: 01-07-2008, 07:45 PM -
Beginner Needs Help w/ Program for School
By badness in forum New To JavaReplies: 2Last Post: 11-24-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks