93 lines
5.4 KiB
HTML
93 lines
5.4 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Consecutive4Equals.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
|
.ST4 {color: #9876aa}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
|
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
|
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST5 {font-family: monospace; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Exams/Exam2_mvn_Practice_CalebFontenot/src/main/java/com/calebfontenot/exam2_mvn_practice_calebfontenot/Consecutive4Equals.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.exam2_mvn_practice_calebfontenot;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> Consecutive4Equals {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
|
{
|
|
java.util.Scanner input = <span class="literal">new</span> java.util.Scanner(System.<span class="ST3">in</span>);
|
|
<span class="literal">while</span> (<span class="literal">true</span>) {
|
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter the number of values, -1 to quit: </span><span class="string">"</span>);
|
|
<span class="literal">int</span> size = input.nextInt();
|
|
<span class="literal">if</span> (size == -<span class="number">1</span>) {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">goodbye!</span><span class="string">"</span>);
|
|
<span class="literal">break</span>;
|
|
}
|
|
<span class="literal">int</span>[] values = <span class="literal">new</span> <span class="literal">int</span>[size];
|
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter the values: </span><span class="string">"</span>);
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < values.<span class="ST4">length</span>; i++) {
|
|
values[i] = input.nextInt();
|
|
}
|
|
<span class="literal">if</span> (<span class="ST5">isConsecutiveFour</span>(values)) {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The list has consecutive fours.</span><span class="string">"</span>);
|
|
} <span class="literal">else</span> {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The list has no consecutive fours.</span><span class="string">"</span>);
|
|
}
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST2">isConsecutiveFour</span>(<span class="literal">int</span>[] values)
|
|
{
|
|
<span class="literal">int</span> numOccurance = <span class="number">0</span>;
|
|
<span class="literal">int</span> iterable = <span class="number">0</span>;
|
|
<span class="literal">int</span> consecutiveFourDetected = <span class="number">0</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < values.<span class="ST4">length</span> - <span class="number">1</span>; i++) {
|
|
<span class="literal">if</span> (values[i] == values[iterable]) {
|
|
numOccurance++;
|
|
<span class="literal">if</span> (i != <span class="number">0</span>) {
|
|
iterable++;
|
|
}
|
|
} <span class="literal">else</span> {
|
|
numOccurance = <span class="number">0</span>;
|
|
}
|
|
<span class="literal">if</span> (numOccurance >= <span class="number">4</span>) {
|
|
consecutiveFourDetected++;
|
|
}
|
|
|
|
}
|
|
<span class="literal">if</span> (consecutiveFourDetected != <span class="number">0</span>) {
|
|
<span class="literal">return</span> <span class="literal">true</span>;
|
|
}
|
|
<span class="literal">else</span> {
|
|
<span class="literal">return</span> <span class="literal">false</span>;
|
|
}
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|