107 lines
6.2 KiB
HTML
107 lines
6.2 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>TwoLargerOfThe3.java</title>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
body {color: #000000; background-color: #ffffff; font-family: Monospaced}
|
||
|
pre {color: #000000; background-color: #ffffff; font-family: Monospaced}
|
||
|
table {color: #000000; background-color: #e9e8e2; font-family: Monospaced}
|
||
|
.ST0 {color: #969696; font-family: Monospaced; font-weight: bold}
|
||
|
.ST1 {font-family: Monospaced; font-weight: bold}
|
||
|
.comment {color: #969696}
|
||
|
.ST2 {font-family: Monospaced; font-weight: bold; font-style: italic}
|
||
|
.ST3 {color: #009900; font-family: Monospaced; font-style: italic}
|
||
|
.literal {color: #0000e6}
|
||
|
.string {color: #ce7b00}
|
||
|
-->
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\ProgrammingExam1_CalebFontenot\src\programmingexam1_calebfontenot\TwoLargerOfThe3.java</td></tr></table>
|
||
|
<pre>
|
||
|
<span class="comment">/*</span>
|
||
|
<span class="comment"> * To change this license header, choose License Headers in Project Properties.</span>
|
||
|
<span class="comment"> * To change this template file, choose Tools | Templates</span>
|
||
|
<span class="comment"> * and open the template in the editor.</span>
|
||
|
<span class="comment"> */</span>
|
||
|
<span class="literal">package</span> programmingexam1_calebfontenot;
|
||
|
|
||
|
<span class="literal">import</span> java.util.Scanner;
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">ar114</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">class</span> <span class="ST1">TwoLargerOfThe3</span> {
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) {
|
||
|
<span class="comment">// Define vars</span>
|
||
|
<span class="literal">int</span> _1, _2, _3; <span class="comment">// Note to Markou: I find numbers easier to compare in my head than variables, hence my use of them here.</span>
|
||
|
<span class="literal">int</span> firstLargest = 0, secondLargest = 0;
|
||
|
|
||
|
<span class="comment">// Setup scanner</span>
|
||
|
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>);
|
||
|
<span class="literal">while</span> (<span class="literal">true</span>) {
|
||
|
<span class="comment">// Prompt for input</span>
|
||
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter 3 numbers: </span><span class="string">"</span>);
|
||
|
_1 = input.nextInt();
|
||
|
_2 = input.nextInt();
|
||
|
_3 = input.nextInt();
|
||
|
|
||
|
<span class="comment">// Compute!</span>
|
||
|
<span class="literal">if</span> (_1 <= _3 & _2 <= _3) { <span class="comment">// Is _3 the largest?</span>
|
||
|
firstLargest = _3; <span class="comment">// Yes!</span>
|
||
|
<span class="literal">if</span> (_2 >= _1) { <span class="comment">// Is _2 larger than _1?</span>
|
||
|
secondLargest = _2; <span class="comment">//Then _2 must be the second largest.</span>
|
||
|
} <span class="literal">else</span> {
|
||
|
secondLargest = _1; <span class="comment">//Then _1 must be the second largest.</span>
|
||
|
}
|
||
|
}
|
||
|
<span class="literal">if</span> (_3 <= _2 & _1 <= _2) { <span class="comment">// Is _2 the largest?</span>
|
||
|
firstLargest = _2; <span class="comment">// Yes!</span>
|
||
|
<span class="literal">if</span> (_3 >= _1) { <span class="comment">//Is _3 larger than _1?</span>
|
||
|
secondLargest = _3; <span class="comment">// Then _3 must be the largest.</span>
|
||
|
} <span class="literal">else</span> {
|
||
|
secondLargest = _1; <span class="comment">// Then _1 must be the largest.</span>
|
||
|
}
|
||
|
}
|
||
|
<span class="literal">if</span> (_3 <= _1 & _2 <= _1) { <span class="comment">// Is _1 the largest?</span>
|
||
|
firstLargest = _1; <span class="comment">// Yes!</span>
|
||
|
<span class="literal">if</span> (_3 >= _2) {<span class="comment">// Is _3 larger than _2?</span>
|
||
|
secondLargest = _3; <span class="comment">// Then _3 must be the largest.</span>
|
||
|
} <span class="literal">else</span> {
|
||
|
secondLargest = _2; <span class="comment">// Then _2 must be the largest.</span>
|
||
|
}
|
||
|
}
|
||
|
<span class="literal">if</span> (_1 == _2 | _1 == _3 | _2 == _3) {
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Some numbers equal. Running additional checks...</span><span class="string">"</span>);
|
||
|
<span class="literal">if</span> (_1 == _2) {
|
||
|
<span class="literal">if</span> (_1 > _3) { <span class="comment">// Is _3 smaller than _1 and _2?</span>
|
||
|
firstLargest = _1;
|
||
|
secondLargest = _3;
|
||
|
}
|
||
|
}
|
||
|
<span class="literal">if</span> (_1 == _3) {
|
||
|
<span class="literal">if</span> (_3 > _2) { <span class="comment">// Is _2 smaller than _1 and _3? </span>
|
||
|
firstLargest = _3;
|
||
|
secondLargest = _2;
|
||
|
}
|
||
|
}
|
||
|
<span class="literal">if</span> (_2 == _3) {
|
||
|
<span class="literal">if</span> (_3 > _1) { <span class="comment">// Is _1 smaller than _2 or _3?</span>
|
||
|
firstLargest = _3;
|
||
|
secondLargest = _1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
<span class="comment">// Output</span>
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The first largest is </span><span class="string">"</span> + firstLargest + <span class="string">"</span><span class="string"> and the second largest is </span><span class="string">"</span> + secondLargest);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
</pre></body>
|
||
|
</html>
|