Your program should proceed as follows: Display a welcome message. Prompt the us
ID: 3805055 • Letter: Y
Question
Your program should proceed as follows:
Display a welcome message.
Prompt the user for an integer which is 3. If the number entered is < 3, keep prompting
the user until they enter a number 3 (use a do/while). This number will determine the
size of the square array.
Fill the array as per pattern 1 and display it using printf to format the array.
Fill the same array as per pattern 2 and display it using printf to format the array.
Display a closing message.
1 2 3 5 10 6 7 8 9 14 15 11 12 13 18 19 20 16 17 19 18 17 16 22 23 24 25 21 24 25 Pattern 1 Pattern 2 (Some slots are shaded to help vou see the pattern. Figure 2-Illustrations of patterns Figures 3 and 4 are screen captures to illustrate the expected behavior of your program. Array Pattern many rows/columns do you want your array to have? (Must be at least 3): Pattern number 1: 12 11 10 9 8 7 13 14 15 16 17 18 24 23 22 21 20 19 25 26 27 28 29 30 36 35 34 33 32 31 Pattern number 2: 2 4 5 6 12 7 8 9 10 11 17 18 13 14 15 16 22 23 24 19 20 21 27 28 29 300 25 26 32 33 34 35 36 31 All doneExplanation / Answer
ArrayPattern.java
import java.util.Scanner;
public class ArrayPattern {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("[--------------------------------]");
System.out.println("[ Array Pattern ]");
System.out.println("[--------------------------------]");
int n =0 ;
do {
System.out.print("How many rows/columns do you want your array to have? ");
n = scan.nextInt();
}while(n < 3);
int a[][]= new int[n][n];
for(int i=0, k=1; i<n; i++){
if(i%2== 0){
for(int j=0; j<n; j++,k++){
a[i][j] = k;
}
}
else{
for(int j=n-1 ;j>=0; j--,k++){
a[i][j] = k;
}
}
}
System.out.println("Pattern Number 1: ");
printArray(a);
for(int i=0, k=1; i<n; i++){
for(int j=i; j<n; j++,k++){
a[i][j] = k;
}
for(int j=0 ;j<i; j++, k++){
a[i][j] = k;
}
}
System.out.println("Pattern Number 2: ");
printArray(a);
System.out.println("All done!!!");
}
public static void printArray(int a[][]){
for(int i=0; i<a.length; i++){
for(int j=0; j<a[i].length; j++){
System.out.printf("%d ",a[i][j]);
}
System.out.println();
}
}
}
Output:
[--------------------------------]
[ Array Pattern ]
[--------------------------------]
How many rows/columns do you want your array to have? 6
Pattern Number 1:
1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 16 17 18
24 23 22 21 20 19
25 26 27 28 29 30
36 35 34 33 32 31
Pattern Number 2:
1 2 3 4 5 6
12 7 8 9 10 11
17 18 13 14 15 16
22 23 24 19 20 21
27 28 29 30 25 26
32 33 34 35 36 31
All done!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.