103 lines
7.1 KiB
HTML
103 lines
7.1 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>IfElse4.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; font-weight: bold}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
|
.literal {color: #cc7832}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/lab6_CalebFontenot/src/main/java/com/calebfontenot/lab6_calebfontenot/IfElse4.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.lab6_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> IfElse4 {
|
|
|
|
<span class="literal">static</span> <span class="literal">double</span> <span class="ST1">inputPrompt</span>() {
|
|
<span class="literal">double</span> grade;
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter your exam grade for it to be letter graded: </span><span class="string">"</span>);
|
|
grade = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>).nextDouble();
|
|
<span class="literal">return</span> grade;
|
|
}
|
|
<span class="literal">static</span> <span class="literal">void</span> <span class="ST1">ifLogic</span>(<span class="literal">double</span> grade) {
|
|
<span class="comment">// Use if else logic and output</span>
|
|
<span class="literal">if</span> (grade > <span class="number">100</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Your grade seems too high, please try again...</span><span class="string">"</span>);
|
|
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(<span class="string">"</span><span class="string">Invalid grade entered!</span><span class="string">"</span>);
|
|
}
|
|
<span class="literal">else</span> <span class="literal">if</span> ( grade > <span class="number">90</span>)
|
|
{
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Congrats! You got a </span><span class="string">"</span> + grade + <span class="string">"</span><span class="string">, which means you got an A!</span><span class="string">"</span>);
|
|
}
|
|
<span class="literal">else</span> <span class="literal">if</span> ( grade > <span class="number">80</span>)
|
|
{
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Good job! You got a </span><span class="string">"</span> + grade + <span class="string">"</span><span class="string">, which means you got a B!</span><span class="string">"</span>);
|
|
}
|
|
<span class="literal">else</span> <span class="literal">if</span> ( grade > <span class="number">70</span>)
|
|
{
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Not bad, you got a </span><span class="string">"</span> + grade + <span class="string">"</span><span class="string">, which means you got a C.</span><span class="string">"</span>);
|
|
}
|
|
<span class="literal">else</span> <span class="literal">if</span> ( grade > <span class="number">60</span>)
|
|
{
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">You got a </span><span class="string">"</span> + grade + <span class="string">"</span><span class="string">, which means you got a D.</span><span class="string">"</span>);
|
|
}
|
|
<span class="literal">else</span> <span class="literal">if</span> (grade < <span class="number">0</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Your grade seems too low, please try again...</span><span class="string">"</span>);
|
|
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(<span class="string">"</span><span class="string">Invalid grade entered!</span><span class="string">"</span>);
|
|
}
|
|
<span class="literal">else</span>
|
|
{
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Sorry, you got a </span><span class="string">"</span> + grade + <span class="string">"</span><span class="string">, so you unfortunately got an F.</span><span class="string">"</span>);
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
|
<span class="comment">// Setup variables</span>
|
|
<span class="literal">double</span> grade;
|
|
String <span class="comment">letterGrade</span>;
|
|
|
|
<span class="literal">while</span> (<span class="literal">true</span>) { <span class="comment">// Unintended behavior: This will only run twice. I intended this try and catch to run indefinately, but it only will catch once before terminating on its own.</span>
|
|
<span class="comment">// Call ifLogic function</span>
|
|
<span class="literal">try</span> {
|
|
<span class="comment">// Prompt for input</span>
|
|
grade = <span class="ST3">inputPrompt</span>();
|
|
<span class="ST3">ifLogic</span>(grade);
|
|
System.<span class="ST3">exit</span>(<span class="number">0</span>);
|
|
}
|
|
<span class="literal">catch</span>(Exception IllegalArgumentException) { <span class="comment">// ifLogic failed, try again...</span>
|
|
<span class="comment">// Prompt for input</span>
|
|
grade = <span class="ST3">inputPrompt</span>();
|
|
<span class="ST3">ifLogic</span>(grade);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|