My assignment was to make a program that takes in a number. That number is then used to construct a pyramid of astericks in several different orientations. We can only use that scanner, and no strings or arrays...
Here's the thing. I have created the full working program and it works great! I was about to hand it in when I found out that we cannot use for loops. It was not written in the assignment that we couldn't... so I spent hours creating this program only to find that we can only use nested while loops.
Is it possible to convert all of these for loops into while loops? With my knowledge of while loops it would require that I take out all of those int's from the for loops and put them outside of the while loops. I have a feeling that I am screwed and will need to re-write my loops....
Here's the thing. I have created the full working program and it works great! I was about to hand it in when I found out that we cannot use for loops. It was not written in the assignment that we couldn't... so I spent hours creating this program only to find that we can only use nested while loops.
Is it possible to convert all of these for loops into while loops? With my knowledge of while loops it would require that I take out all of those int's from the for loops and put them outside of the while loops. I have a feeling that I am screwed and will need to re-write my loops....
import java.util.*;
public class Triangles
{
private static final int TWO_2 = 2; //Declared variabes to avoid
private static final int ONE_1 = 1; //hard coding penalty.
public static void main(String[] args)
{
Scanner input = new Scanner (System.in); //Takes in input
System.out.print("Enter the size of the triangles to display: ");
while ( input.hasNext() ) //first while, while input...
{
int total = input.nextInt(); //total equals users input
if( total < TWO_2 ) //error loop for inputs < 2
{
System.out.println("Triangle size must be > 1; Try again.\n");
System.out.print("Enter the size of the triangles to display: ");
}
else if( total >= TWO_2 ) //if larger than 2, continue..
{
for ( int i = ONE_1; i <= total; i++ ) //loop for total lines
{
for ( int j = ONE_1; j <= i; j++) //Series of loops that-
{ //work to place asterisk
System.out.print( "*" ); //first triangle
}
for ( int s = ( total ); s >= i; s-- )
{
System.out.print( " " ); //empty space
}
for ( int s = ( total ); s >= i; s-- )
{
System.out.print( "*" ); //reversed triangle
}
for ( int j = ONE_1; j <= i; j++)
{
System.out.print( " " ); //empty space
}
for ( int j = TWO_2; j <= i; j++)
{
System.out.print( " " ); //second empty space
}
for ( int s = ( total ); s >= i; s-- )
{
System.out.print( "*"); //reverse-flipped triangle
}
for ( int s = ( total ); s >= i; s-- )
{
System.out.print( " " ); //empty space
}
for ( int j = ONE_1; j <= i; j++)
{
System.out.print( "*" ); //reverse-reverse-flipped triangle
}
System.out.println();
} //end of nested loop
System.exit(0); //ends the whole loop
} //end of first for loop
} //end of input.hasNext
} //end of main
} //end of class