Reputation: 27
Any ideas how to write Java program in a class named Window that produces the preceding figure as output. I have to use nested for loops to print the repeated parts of the figure. I've tried lots of times, no success :(
Write a Java program in a class named Window that produces the preceding figure as output. Use nested for loops to print the repeated parts of the figure. Once you get it to work, add one class constant to your program so that the size of the figure can be changed simply by changing that constant's value. The example output shown is at a constant size of 3, but if you change the constant, the figure should grow larger and wider proportionally.
+===+===+
| | |
| | |
| | |
+===+===+
| | |
| | |
| | |
+===+===+
OK i've got this, but still need to get rid of 3 bottom lines - any idea?
for (int i = 1; i <= 3; i++) {
for (int plus = 1; plus <= 2; plus++) {
System.out.print("+");
for (int shave = 1; shave <= 3; shave++) {
System.out.print("=");
}
}
System.out.print("+");
System.out.println();
for (int time = 1; time <= 3; time++) {
for (int kav = 1; kav <= 3; kav++) {
System.out.print("|");
for (int rev = 1; rev <= 3; rev++) {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Upvotes: 1
Views: 4446
Reputation: 1
This is simplest way.
public class Window {
public static final int size = 3;
public static void main(String[] args) {
for (int x=0; x<2; x++) {
for (int y = 0; y<2; y++){
System.out.print("+");
for (int z=0;z<size; z++) {
System.out.print("=");
}
}
System.out.println("+");
for (int i = 0; i<size; i ++) {
for (int j = 0; j<2; j++) {
System.out.print("|");
for (int k = 0; k<size; k++) {
System.out.print(" ");
}
}
System.out.println("|");
}
}
for (int y = 0; y<2; y++){
System.out.print("+");
for (int z=0;z<size; z++) {
System.out.print("=");
}
}
System.out.println("+");
}
}
Upvotes: 0
Reputation: 1
public class Window{
public static final int size=3;
public static void main(String[] args){
for (int p = 1; p <= 2; p++) {
for (int i = 1; i <=2; i++) {
System.out.print("+");
for (int j = 1; j <= size; j++) {
System.out.print("=");
}
}
System.out.print("+");
System.out.println();
for (int k = 1; k <= size; k++) {
for (int i = 1; i <= 3; i++) {
System.out.print("|");
for (int j = 1; j <= size; j++) {
System.out.print(" ");
}
}
System.out.println();
}
}
for (int i = 1; i <= 2; i++) {
System.out.print("+");
for (int j = 1; j <= size; j++) {
System.out.print("=");
}
}
System.out.print("+");
}
}
Upvotes: -1
Reputation: 194
I was going through random questions and I found no one actually answered correctly by correcting the code you posted. So here I post my code with nested for loops and it satisfies all your criteria, giving the correct output.
int BLOCK_SIZE = 4;
for (int i=1; i<BLOCK_SIZE; i++){
for(int j=1; j<BLOCK_SIZE; j++){
System.out.print("+===");
}
System.out.println("+");
for(int k=0; k<3; k++){
for(int j=1; j<BLOCK_SIZE; j++){
System.out.print("| ");
}
System.out.println("|");
}
}
for(int j=1; j<BLOCK_SIZE; j++){
System.out.print("+===");
}
System.out.println("+");
Another approach using some if statements
Here I make use of the fact that every (4n+1) line has '+' replacing '|' and '=' replacing 'whitespace'
int BLOCK_SIZE = 5;
int length = BLOCK_SIZE*4-4;
char one,two;
for(int i=0; i<=length; i++){
if(i%4!=0){one='|';two=' ';}
else{one='+';two='=';}
for(int j=0; j<=length; j++){
if(j%4==0) {
System.out.print(one);
}
else {
System.out.print(two);
}
}
System.out.println();
}
Granted it's a homework problem, but we can have fun cracking them even if they serve no real life purpose. Such is the happiness that can be gotten from coding! :)
Upvotes: 1
Reputation: 6277
I think this is what you are Looking for:
final int BLOCK_SIZE = 2;
for(int i=0; i<1; i++){
System.out.print("+===+");
for(int j=0; j<1; j++){
System.out.println("===+");
for(int k=0; k<BLOCK_SIZE; k++){
System.out.println("| | |\n| | |\n| | |");
for(int l=0; l<1; l++){
System.out.println("+===+===+");
}
} System.out.println();
}
}
Upvotes: 3
Reputation: 1253
Could be refactored to be more pretty, but will work:
static String[][] array = { { "x", "="}, { "|", " "}};
public static void main(String[] args) {
for(int y = 0; y < 9; y++) {
for(int x = 0; x < 9; x++) {
System.out.print(getSign(y, x));
}
System.out.print("\n");
}
}
private static String getSign(int y, int x) {
int modY = y % 4;
int modX = x % 4;
return array[getPos(modY)][getPos(modX)];
}
private static int getPos(int mod) {
return (mod & 1) | ((mod & 2) >> 1);
}
Upvotes: 0
Reputation: 5064
Took sometime to achieve your expected result, see if it works for you?
public class Homework
{
/**
* @param args
*/
public static void main(String[] args)
{
for (int line = 1; line <= 3; line ++)
{
NEWLINE:
for (int plus = 1; plus <= 3; plus++)
{
System.out.print("+");
if (plus == 3)
{
for (int k = 1; k <= 3; k++)
{
if (line == 3)
{
break NEWLINE;
}
System.out.println("");
System.out.print(" | | |");
if (k == 3)
{
System.out.println();
break NEWLINE;
}
}
}
for (int eq = 1; eq <= 6; eq++)
{
if (eq % 4 == 0)
{
break;
}
System.out.print("=");
}
}
}
}
}
Upvotes: 1
Reputation: 26167
Here is one way
int rows = 3;
int cols = 4;
String output1 = "";
String output2 = "";
for(int j = 0; j < cols; j++)
{
output1 += "| ";
}
output1 += "|";
for(int j = 0; j < cols; j++)
{
output2 += "+===";
}
output2 += "+";
for(int i = 0; i < rows*4; i++)
{
System.out.println((i % 4 != 0)?output1:output2);
}
System.out.println(output2);
Upvotes: 0