Thursday, November 15, 2012
Javascript Nested While Loops
//CSC 15 lab 6 Nested While Loops
//lab6.java
//Christopher Smith
//
import java.util.Scanner;
public class lab6 {
public static void main (String[] args) {
Scanner kb = new Scanner(System.in); //defining keyboard scanner
int x; //defining variables
int row_count;
int col_count;
int item_count;
System.out.println("Enter an integer (>0):"); //screen prompt for integer greater than zero
x = kb.nextInt(); //input for x
if (x<0) //verifying the user input for fun
{
System.out.println("I said greater than 0!!");
System.out.println("Please input another value");
x = kb.nextInt();
}
//starting code for patterns
//uses user defined x to define the dimensions
//Fig 1: 1 row with x columns--------------------------------------------------------------------
System.out.println("Here is x defined columns for 1 row");
col_count = 1;
while( col_count <= x) //while columns are less than or = to x
{
System.out.print("*"); //prints * for each column
col_count++; //increments the columns
}
System.out.println();
//Fig 2: A square with x defined width and height------------------------------------------------
System.out.println("Here is a square with user defined height and width");
row_count = 1;
while( row_count <= x ) //row count starting at 1 staying under x
{
col_count = 1;
while( col_count <= x ) // columns fill the row while under x, row by row
{
System.out.print( "*" );
col_count = col_count + 1;//incrementing columns
}
System.out.println();
row_count = row_count + 1; //incrementing rows
}
//Fig 3: Squares counting up to the user defined amount of width and height-------------------------------------
System.out.println("Figure 3");
item_count = 1;//item count tracks number of squares and determines width and height of squares
while(item_count<=x)
{
row_count = 1;
while(row_count<=item_count)//using item count instead of x
{
col_count = 1;
while(col_count<=item_count)//using item count instead of x
{
System.out.print("*");
col_count = col_count + 1;//incrementing for loop
}
System.out.println();
row_count = row_count + 1;//incrementing
}
System.out.println();
item_count = item_count + 1;//incrementing
}
//Figure 4: Triangle counting down column = to row number-------------------------------------------------------------------
System.out.println("Figure 4 triangle");
row_count = 1;
while (row_count<=x)
{
col_count = 1;
while (col_count <= row_count)//if row is 2, this prints for column 1 and 2 within row
{
System.out.print("*");
col_count++;//increment column
}
row_count++; //increment row
System.out.println();
}
//Figure 5: Starting at x defined and counting down to 1 triangle-----------------------------------------------------
System.out.println("Starting figure 5");
row_count = 1;
while (row_count<=x)
{
col_count = x; //have to start with user defined value for x
while (col_count >= row_count)//using row to define amount of columns
{
System.out.print("*");
col_count--; //incrementing columns to fill in row
}
row_count++; //going down one for rows
System.out.println();
}
//Figure 6: Hollow square------------------------------------------------
System.out.println("Figure 6");
int a = 0, b = 0;
while (a <= x) {
if (a == 0 || a == x) //Is it top and bottom or inside of square
{
col_count = 1;
while( col_count <= x) //while to print out the top and bottom line
{
System.out.print("*"); //prints * for each column
col_count++; //increments the columns
}
}
else //inside of square
{
b = 0;
while (b <= x)
{
if (b == 0 || b == x)
{
System.out.print("*");//prints side of square
}
else
{
System.out.print(" ");//prints blank spaces sequence
}
b++;
}
}
System.out.print("\n");
a++;
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment