Building Java Programs A Back To Basics Approach - 3rd Edition
by Stuart Reges and Marty Stepp
Chapter 2 Exercise 4
Write nested for loops to produce the following output:
+
+++
+++++
+++++++
/* Copyright Mihai George Forlafu @ 2014
* Copenhagen School of Design and Technology
* Building Java Programs A Back To Basics Approach 5th edition
* Chapter 2 Exercise 4, page 156
*/
package ch2_ex4_v2;
public class Ch2_Ex4_v2
{
public static void main(String[] args)
{
for (int line = 4; line >= 1; line--)
{
for (int space = 1; space <= line-1; space++)
{
System.out.print(" ");
}
for (int pluses = 1; pluses<=9-2*line; pluses++)
{
System.out.print("+");
}
System.out.println("");
}
}
}
Another solution for this exercise is the one below. This solution fulfill all the requirements of the exercise but as you can see it takes to many lines of code. It is a very rudimentary way of solving the exercise.
package ch2_ex4;
public class Ch2_Ex4
{
public static void main(String[] args)
{
for (int line = 1; line <= 1; line=line+1)
{
for (int space = 3; space >=1; space = space-1)
{
System.out.print(" ");
}
for (int plus = 1; plus <= 1; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
for (int line = 1; line <= 1; line=line+1)
{
for (int space = 2; space >=1; space = space-1)
{
System.out.print(" ");
}
for (int plus = 1; plus <= 3; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
for (int line = 1; line <= 1; line=line+1)
{
for (int space = 1; space >=1; space = space-1)
{
System.out.print(" ");
}
for (int plus = 1; plus <= 5; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
for (int line = 1; line <= 1; line=line+1)
{
for (int plus = 1; plus <= 7; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
}
by Stuart Reges and Marty Stepp
Chapter 2 Exercise 4
Write nested for loops to produce the following output:
+
+++
+++++
+++++++
/* Copyright Mihai George Forlafu @ 2014
* Copenhagen School of Design and Technology
* Building Java Programs A Back To Basics Approach 5th edition
* Chapter 2 Exercise 4, page 156
*/
package ch2_ex4_v2;
public class Ch2_Ex4_v2
{
public static void main(String[] args)
{
for (int line = 4; line >= 1; line--)
{
for (int space = 1; space <= line-1; space++)
{
System.out.print(" ");
}
for (int pluses = 1; pluses<=9-2*line; pluses++)
{
System.out.print("+");
}
System.out.println("");
}
}
}
Another solution for this exercise is the one below. This solution fulfill all the requirements of the exercise but as you can see it takes to many lines of code. It is a very rudimentary way of solving the exercise.
package ch2_ex4;
public class Ch2_Ex4
{
public static void main(String[] args)
{
for (int line = 1; line <= 1; line=line+1)
{
for (int space = 3; space >=1; space = space-1)
{
System.out.print(" ");
}
for (int plus = 1; plus <= 1; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
for (int line = 1; line <= 1; line=line+1)
{
for (int space = 2; space >=1; space = space-1)
{
System.out.print(" ");
}
for (int plus = 1; plus <= 3; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
for (int line = 1; line <= 1; line=line+1)
{
for (int space = 1; space >=1; space = space-1)
{
System.out.print(" ");
}
for (int plus = 1; plus <= 5; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
for (int line = 1; line <= 1; line=line+1)
{
for (int plus = 1; plus <= 7; plus = plus+1)
{
System.out.print("+");
}
System.out.println("");
}
}
0 comments:
Post a Comment