Question

(Java) the question is comments under public static void main(String[] args) {    for(int i =1;...

(Java) the question is comments under

public static void main(String[] args) {

   for(int i =1; i<=5; i++)
   {
       for(int j=1; j<=i; j++)
   {
           System.out.print("* ");
       }
           System.out.println(""); //
          
   }
  
   for(int i=1; i<=4;i++)
   {
       for(int j=4; j>=i; j--) // j>=i means each row has * of columns, so first row ****, second row ***, and so on
           // But, j>=i The thing confusing about this is, when it is 3rd row j is 2 columns, 4th row, 1 column or 1 asterisk. so j isn't greater than i, but why do we use j >= i? I do get the asterisk decrement by 1 so we use j--.
By the way this coding will give half diamond shape.
           {
               System.out.print("* ");
           }
               System.out.println("");
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

import java.util.*;

class Test {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

System.out.print("* ");

}

System.out.println(""); //

}

for (int i = 4; i >= 1; i--) {

for (int j = 1; j <= i; j++)

{

System.out.print("* ");

}

System.out.println("");

}

}

}

/*

I have made changes in the code so that is quiet easy to understand..

i then second half of the loop..

i loops from 4 to 1

when i = 4, j loops from 1 to 4 times so it print 4 *'s

when i = 3, j loops from 1 to 3 times so it print 3 *'s

when i = 2, j loops from 1 to 2 times so it print 2 *'s

when i = 1, j loops from 1 to 1 times so it print 1 *'s

when i = 0 exit the loop

*/

Add a comment
Know the answer?
Add Answer to:
(Java) the question is comments under public static void main(String[] args) {    for(int i =1;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

  • what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int...

    what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int w 0; int x; int y 5; float z = 6.1 System.out.print("Enter y: "); x keyboard.nextint); System.out.println('y'); System.out.println(x); System.out.println(w*3); x- x+(int)z; System.out.println(x); 0 System.out.println(u); System.out.,println(u); System.out.println"x In" + y); System.out.print(y + z); ) liclosing main method 1 liclosing class header

  • import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int...

    import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int temp;                       for (int i = list.length - 1; i > 0; i--)    {    for (int j = 0; j < i; j++)    {    if (list[j] > list[j + 1])    {    temp = list[j];    list[j] = list[j + 1];    list[j + 1] = temp;    }    }    }   ...

  • Review the following code: public class Looping {    public static void main(String[] args) {      ...

    Review the following code: public class Looping {    public static void main(String[] args) {       for (int i = 1; i <= 5; i++) {          for (int j = 1; j <= 5; j++) {             System.out.println(i + " x " + j + " = " + (i * j));          }       }    } } What is the output from the code above? Replace this text with your solution What happens if you change the...

  • Given the following Java code: public static void main (String[] args) { int num; System.out.println("Enter a...

    Given the following Java code: public static void main (String[] args) { int num; System.out.println("Enter a number"); num = scan.nextInt(); <-first input statement while (num != 0) { System.out.println ("The number is: " + num); System.out.println("Enter a number"); num = scan.nextInt(); } //End While } //End Module The first input statement shown above is called the:

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT