Question

Write the following method that tests whether a two-dimensional array has four consecutive numbers of the same value, either horizontally, vertically, or diagonally:

Public static boolean isConsecutiveFour(int[][] values)

Solution

Verified
Answered this week
Answered this week
Step 1
1 of 11

Import necessary java package.

import java.util.Scanner;
 
public class PatternRecognition {
 
 public static void main(String[] args) {
 
  Scanner input = new Scanner(System.in);
  System.out.println(Enter number of rows and column: );
  int[][] n = new int[input.nextInt()][input.nextInt()];
  System.out.println(Enter the values in the array:);
 
  for (int i = 0; i < n.length; i++) {
   for (int j = 0; j < n[i].length; j++) {
    n[i][j] = input.nextInt();
   }
 
  }
   
 System.out.println(isConsecutiveFour(n));
    
   
 
 }
 
 public static boolean isConsecutiveFour(int[][] values) {
  return checkVer(values)||checkHor(values) || checkDia(values);
 }

Create an account to view solutions

Create an account to view solutions

More related questions

1/4

1/7