I am trying to write a program where the user can enter a height and width and it creates a hollow box of asterisks. I can get the stars to print out but what is wrong with my code that it is putting them in weird lines. How do I get them to form into what I want them to?
import java.util.Scanner;
public class Stars
{
public static void main(String args[])
{
// Create a Scanner so we can get input from the user.
Scanner input = new Scanner(System.in);
int starsinarow ; //number of stars up and down
int columns ; //how many across
int columncounter=0; //column counter
int rowCounter=0; // row counter
int starsinarowCounter=0;
//welcome
System.out.println("Welcome! This program creates a box made of stars! The rows and columns must be postive full numbers");
System.out.println("Please enter the number of rows you want: ");
starsinarow = input.nextInt ();
if (starsinarow<0)
{
System.out.println("Please enter a vaild number.");
}
else
{
System.out.println("Please enter how many columns you want: ");
columns = input.nextInt();
//getting the top row
while(columncounter<columns)
{
System.out.print("* ");
columncounter ++;
}
//printing middle stuff
while (rowCounter<starsinarow-2)
{
System.out.println("* ");
if(columns>2)
{
while (columncounter< columns);
{
System.out.print(" ");
columncounter++;
}
}
System.out.print("*");
rowCounter ++;
}
//getting the bottom row
while(columncounter<columns)
{
System.out.print("*");
columncounter ++;
}
}
}
}
whats happeneing on the comman prompt:
C:\Users\jorda\Documents\CSCI201\Assignment3>java Stars
Welcome! This program creates a box made of stars! The rows and columns must be postive full numbers
Please enter the number of rows you want:
6
Please enter how many columns you want:
7
* * * * * * * *
**
**
**
*
C:\Users\jorda\Documents\CSCI201\Assignment3>java Stars
Welcome! This program creates a box made of stars! The rows and columns must be postive full numbers
Please enter the number of rows you want:
8
Please enter how many columns you want:
3
* * * *
**
**
**
**
**
*
C:\Users\jorda\Documents\CSCI201\Assignment