(Display four patterns using loops) Ask the user to enter an integer to
set a limit for nested loops and generate the following four patterns:

Enter an integer to be a limit of the pattern:6
Pattern A:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

Pattern B:
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Pattern C:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1

Pattern D:
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1 Complete answer in java please. this is what i have including error message:

LoopPattern.java:1: error: class, interface, or enum expected
looppattern;
^
1 error


1 looppattern;
2 public class Looppattern{
3 public static void main(String []args){
4 scanner sc = new scanner(System.in);
5 System.out.println("Enter how man levels you need: ");
6 int levels = sc.nextInt();
7 System.out.println("\n---------------Pattern A-----------------\n");
8 for(int p=1;p<=levels;p++){
9 for(int k=1;k<=p;k++){ //increasing each level printing
10 System.out.print(k);
11 }
12 System.out.println();
13 }
14 System.out.println("\n---------------Pattern B-----------------\n");
15 int r=levels;
16 for(int p=1;p<=levels;p++){
17 for(int k=1;k<=r;k++){
18 System.out.print(k);
19 }
20 r--; //decreasing levels
21 System.out.println();
22 }
23 System.out.println("\n---------------Pattern C-----------------\n");
24 for(int p=1;p<=levels;p++){ //here incresing
25 for(int k=p;k>0;k--){ //and here decreasing pattern to achieve our required pattern
26 System.out.print(k);
27 }
28 System.out.println();
29 }
30 System.out.println("\n---------------Pattern D-----------------\n");
31 r=levels;
32 for(int p=1;p<=levels;p++){
33 for(int k=1;k<=r;k++){
34 System.out.print(k);
35 }
36 r--; //decreasing levels
37 System.out.println();
38 }
39 }
40 }

Respuesta :

Answer:

Hi There was small mistake. It is working fine for me. When you run from command line - use LoopPattern, not looppatern

import java.util.Scanner;

public class Looppattern {

  public static void main(String[] args) {

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter how man levels you need: ");

      int levels = sc.nextInt();

      System.out.println("\n---------------Pattern A-----------------\n");

      for (int p = 1; p <= levels; p++) {

          for (int k = 1; k <= p; k++) { // increasing each level printing

              System.out.print(k);

          }

          System.out.println();

      }

      System.out.println("\n---------------Pattern B-----------------\n");

      int r = levels;

      for (int p = 1; p <= levels; p++) {

          for (int k = 1; k <= r; k++) {

              System.out.print(k);

          }

          r--; // decreasing levels

          System.out.println();

      }

      System.out.println("\n---------------Pattern C-----------------\n");

      for (int p = 1; p <= levels; p++) { // here incresing

          for (int k = p; k > 0; k--) { // and here decreasing pattern to

                                          // achieve our required pattern

              System.out.print(k);

          }

          System.out.println();

      }

      System.out.println("\n---------------Pattern D-----------------\n");

      r = levels;

      for (int p = 1; p <= levels; p++) {

          for (int k = 1; k <= r; k++) {

              System.out.print(k);

          }

          r--; // decreasing levels

          System.out.println();

      }

  }

}

Explanation: