I was suppose to create a simple Java program for calculating the area of a rectangle (height * width). Then check the
user’s input, and make sure that they enter in a positive integer, and letting them try again if
they enter in a negative number.(I'm not sure how to get them to try again.
I am suppose to use an "if" statements and indeterminate loops to achieve the solution.
The program will have the following requirements:
1.Ask the user to enter in both a height and width (as an integer)
2. If the user enters in a negative number, display an error
3. If the user enters in a negative number, give them another chance to enter in the
correct value
4. Calculate the area and display to the screen once two positive integers have been
entered.
import java.util.Scanner; public class RectangleAreaCalc { public static void main(String[] args) { int length; int width; int area; Scanner input = new Scanner(System.in); System.out.println("Please enter the length of the rectangle:"); length = input.nextInt(); if (length < 0) { System.out.println("Incorrect Input, Please enter a positive number for length"); } else System.out.println("Please enter the width of the rectangle:"); width = input.nextInt(); if (width <= 0) { System.out.println("Incorrect Input, Please enter a positive number for width"); } else { area = length * width; System.out.println("The area of a rectangle is:" + area); } } }