/home/caleb/ASDV-Java/Exams/Exam2_mvn_Practice_CalebFontenot/src/main/java/com/calebfontenot/exam2_mvn_practice_calebfontenot/Consecutive4Equals.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.exam2_mvn_practice_calebfontenot;
@author
public class Consecutive4Equals {
public static void main(String[] args)
{
java.util.Scanner input = new java.util.Scanner(System.in);
while (true) {
System.out.print("Enter the number of values, -1 to quit: ");
int size = input.nextInt();
if (size == -1) {
System.out.println("goodbye!");
break;
}
int[] values = new int[size];
System.out.print("Enter the values: ");
for (int i = 0; i < values.length; i++) {
values[i] = input.nextInt();
}
if (isConsecutiveFour(values)) {
System.out.println("The list has consecutive fours.");
} else {
System.out.println("The list has no consecutive fours.");
}
}
}
public static boolean isConsecutiveFour(int[] values)
{
int numOccurance = 0;
int iterable = 0;
int consecutiveFourDetected = 0;
for (int i = 0; i < values.length - 1; i++) {
if (values[i] == values[iterable]) {
numOccurance++;
if (i != 0) {
iterable++;
}
} else {
numOccurance = 0;
}
if (numOccurance >= 4) {
consecutiveFourDetected++;
}
}
if (consecutiveFourDetected != 0) {
return true;
}
else {
return false;
}
}
}