Code:
{
static Scanner data_input = new Scanner(System.in);
// Method to calculate the area of a square
static void square(String [] args)
{
int l, n, i, t, result = 0;
boolean quit;
// Get the length
System.out.print("Enter the length of a side of the square: ");
l = data_input.nextInt();
while(l <=0)
{
// Output error message
System.out.println("Length must be greater than 0");
// Get length
System.out.print("Enter the length: ");
l = data_input.nextInt();
}
// Get the number of squares
System.out.print("Enter the number of squares you wish to calculate: ");
n = data_input.nextInt();
if (n <=0)
{
System.out.println("That number was not valid:");
quit = true;
// Get the number of squares
System.out.print("Enter the number of squares you wish to calculate: ");
n = data_input.nextInt();
}
// Get the Increment
System.out.print("Enter the size increment: ");
i = data_input.nextInt();
if (i <=0)
{
System.out.println("That number was not valid:");
quit = true;
// Get the Increment
System.out.print("Enter the size increment: ");
i = data_input.nextInt();
}
// Add the numbers
for(t = 0 ; t < n; t++ )
{
result = (l * l);
// Display the result
System.out.print(l + " * ");
System.out.print(l + " = ");
System.out.println(result);
l = l + i;
}
}
Comment