In this section, we\'ll produce more complex output by wrapping one loop inside
ID: 3927073 • Letter: I
Question
In this section, we'll produce more complex output by wrapping one loop inside another. This so-called nesting produces more complex results at the cost of increasing program complexity, and in the worst case can make some programs intractable. We'll limit our degree of nesting to k == 2 here to get started. Build a short, standalone program with the class name StraightLine that produces a straight line, just as in loop 0 in section 2 above. Use a loop variable called size to terminate the loop. The output should look like: ***//size == 3 *****//size === 5 Now, wrap this loop that produces a line inside another loop, as demonstrated in loop 1 in section 2. Make the outer loop also terminate using the "size" variable. The only thing to add to the outer loop is a System.out.println (); to move the cursor to the next line. When you're done, the output should look like the square below, with the same number of rows and columns. ***//size == 3 *** *** *****//size == 5 ***** *****Explanation / Answer
package org.students;
import java.util.Scanner;
public class StraightLine {
public static void main(String[] args) {
// Scanner class object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
// Getting the number entered by the user
System.out.print("Enter a number :");
int size = sc.nextInt();
System.out.println(" ");
// Displaying the Lines
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
System.out.print("* ");
}
System.out.println(" ");
}
}
}
____________________________________
output:
Enter a number :4
* * * *
* * * *
* * * *
* * * *
______________________________
output:
Enter a number :5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.