Actually, I have the code for a square program that does similar to the same thing.
Rectangle is more confusing because I need to get 2 inputs, height and width, and determine the rectangle from there, which I'm stuck at, if someone can check out the square code and figure something out for a rectangle. Here it is..
- import java.util.Scanner;
-
-
/**********************************************
-
* *
-
* Prints a square of size *
-
* given by the user. *
-
* Uses the drawLine() method. *
-
* *
-
**********************************************/
-
-
public class Square {
-
-
public static void main(String[] args) {
-
-
int squareSize;
-
-
do {
-
-
squareSize = getSquareSize();
-
drawSquare(squareSize);
-
System.out.println();
-
-
} while (squareSize >= 0);
-
-
}
-
-
/*
-
* Prompts user to input number
-
* returns input as size of square to print
-
*/
-
public static int getSquareSize() {
-
-
int squareInput;
-
Scanner myScanner = new Scanner(System.in);
-
System.out.print("Enter square size (negative to exit): ");
-
squareInput = myScanner.nextInt();
-
-
return squareInput;
-
-
}
-
-
/*
-
* Prints a square of given size
-
*/
-
public static void drawSquare(int size) {
-
-
for (int i=1; i<=size; i++) {
-
-
drawLine(size);
-
System.out.println();
-
-
}
-
-
}
-
-
/*
-
* Prints a line of given length
-
*/
-
public static void drawLine(int length) {
-
-
for (int i=1; i<=length; i++)
-
System.out.print("*");
-
-
}
-
-
}