ASDV-Java/Semester 1/Exams/Exam2_CalebFontenot/Problem3.html

76 lines
4.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Problem3.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\Exam2_CalebFontenot\src\exam2_calebfontenot\Problem3.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> exam2_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">Problem3</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">// Create scanner</span>
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>);
<span class="comment">// Define args</span>
<span class="literal">int</span> currentPrime = 0, timesToIterate = 0, timesIncremented = 0;
<span class="literal">while</span> (timesToIterate != -1) {
System.<span class="ST3">out</span>.print(<span class="string">&quot;</span> <span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">===&gt; -1 to quit or enter the number of primes you need generated and i will print 10 per line: </span><span class="string">&quot;</span>);
<span class="comment">// Get amount of times the user wants us to iterate for.</span>
timesToIterate = input.nextInt();
<span class="comment">// Don&#39;t do anything if the input is -1</span>
<span class="literal">if</span> (timesToIterate != -1) {
<span class="comment">// First loop. This determines how many times to iterate.</span>
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt;= timesToIterate; i++) {
currentPrime++;
<span class="comment">// It&#39;s broken somewhere here, but I&#39;m not sure where exactly...</span>
<span class="comment">// Generate primes by incrementing currentPrime and attempting to divide by itself. </span>
<span class="literal">for</span> (<span class="literal">int</span> j = 1; j &lt; timesIncremented; j++) {
<span class="literal">if</span> (currentPrime % j == 0) {
System.<span class="ST3">out</span>.print(currentPrime + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
timesIncremented++;
<span class="comment">// If we&#39;ve incremented 10 times, print a new line.</span>
<span class="literal">if</span> (timesIncremented % 10 == 0) {
System.<span class="ST3">out</span>.println();
}
}
}
}
}
}
}
</pre></body>
</html>