71 lines
4.0 KiB
HTML
71 lines
4.0 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>SubtractionQuiz.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}
|
||
|
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||
|
.comment {color: #808080}
|
||
|
.whitespace {color: #505050}
|
||
|
.number {color: #6897bb}
|
||
|
.string {color: #6a8759}
|
||
|
.literal {color: #cc7832}
|
||
|
-->
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<table width="100%"><tr><td align="center">/home/caleb/NetBeansProjects/ADSV Java/lab7_CalebFontenot/src/main/java/com/calebfontenot/lab7_calebfontenot/SubtractionQuiz.java</td></tr></table>
|
||
|
<pre>
|
||
|
<span class="comment">/*</span>
|
||
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
|
||
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
||
|
<span class="comment"> */</span>
|
||
|
<span class="literal">package</span> com.calebfontenot.lab7_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">caleb</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">class</span> SubtractionQuiz {
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
||
|
{
|
||
|
<span class="comment">// Create scanner outside of loop so we don't create it over and over again</span>
|
||
|
Scanner input = <span class="literal">new</span> Scanner(System.in);
|
||
|
<span class="literal">while</span>(<span class="literal">true</span>) {
|
||
|
<span class="comment">// 1. Generate two random single-digit integers</span>
|
||
|
<span class="literal">int</span> number1 = (<span class="literal">int</span>)(Math.random() * <span class="number">10</span>);
|
||
|
<span class="literal">int</span> number2 = (<span class="literal">int</span>)(Math.random() * <span class="number">10</span>);
|
||
|
|
||
|
<span class="comment">// 2. If number < number2, swap number1 with number2</span>
|
||
|
<span class="literal">if</span> (number1 < number2)
|
||
|
{
|
||
|
<span class="literal">int</span> temp = number1;
|
||
|
number1 = number2;
|
||
|
number2 = temp;
|
||
|
}
|
||
|
|
||
|
<span class="comment">// 3. Prompt the student to answer "what is number1 - number2?"</span>
|
||
|
System.out.print(<span class="string">"</span><span class="string">What is </span><span class="string">"</span> + number1 + <span class="string">"</span><span class="string"> - </span><span class="string">"</span> + number2 + <span class="string">"</span><span class="string">? </span><span class="string">"</span>);
|
||
|
<span class="literal">int</span> answer = input.nextInt();
|
||
|
|
||
|
<span class="comment">// 4. Grade the answer and display the result</span>
|
||
|
<span class="literal">if</span> (number1 - number2 == answer)
|
||
|
System.out.println(<span class="string">"</span><span class="string">You are correct!</span><span class="string">"</span>);
|
||
|
<span class="literal">else</span>
|
||
|
{
|
||
|
System.out.println(<span class="string">"</span><span class="string">Your answer is wrong.</span><span class="string">"</span>);
|
||
|
System.out.println(number1 + <span class="string">"</span><span class="string"> - </span><span class="string">"</span> + number2 + <span class="string">"</span><span class="string">should be</span><span class="string">"</span> + (number1 - number2));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
</pre></body>
|
||
|
</html>
|