In: Computer Science
JAVA
If a user inputs a specific number of rows, columns and sections for a rectangle using three symbols, how would you print using for loops and a string? Not using an array
Example:
If 4 was inputted for rows, 12 was inputted for columns, 6 was inputted for sections, and the following symbols (that are all chosen by the user) were “$” “*” and “%” the following would print:
$$**%%$$**%%
$$**%%$$**%%
$$**%%$$**%%
$$**%%$$**%%
Has to account for all numbers including odd. And the three symbols repeat.
Below is the code for the above code:
import java.util.Scanner;
public class Rectangle {
/**main method to start the program*/
public static void main(String[] args) {
int rows;
int columns;
int section;
char symbol1;
char symbol2;
char symbol3;
/**taking input from the
users*/
Scanner input = new
Scanner(System.in);
System.out.println("Enter the
number of rows");
rows = input.nextInt();
System.out.println("Enter the
number of columns");
columns = input.nextInt();
System.out.println("Enter the
sections");
section = input.nextInt();
System.out.println("Enter the
symbol 1");
symbol1 =
input.next().charAt(0);
System.out.println("Enter the
symbol 2");
symbol2 =
input.next().charAt(0);
System.out.println("Enter the
symbol 3");
symbol3 =
input.next().charAt(0);
int innerlooptimes = (int)
Math.ceil(section/3);
/**outer loop will run for row
times*/
for(int i = 0;i<rows; i++
)
{
/**2nd inner
class will run for column/section times*/
for(int j=0;
j<(int)Math.ceil(columns/section); j++)
{
/**there are 3 symbols, so 3rd inner loop will
run for 3 times*/
for(int k=0;k<3;k++)
{
if(k%3==0)
{
/**most
inner loop will run for section/3 times*/
for(int p
=0; p<innerlooptimes;p++)
System.out.print(symbol1);
}
else if(k%3==1)
{
for(int p
=0; p<innerlooptimes;p++)
System.out.print(symbol2);
}
else
{
for(int p
=0; p<innerlooptimes;p++)
System.out.print(symbol3);
}
}
}
System.out.println();
}
/**if you calculate number of time
inner loops will run , (Column/section)*(3)*(section/3) =
columns
* */
}
}
output: