My assignment is to build a prime number generator using nested loops. If the number is prime, it gets concatenated to a String object. The program outputs the results into a message box using JOptionPane. No user input is needed.
My code compiles, but every single number starting from 2 gets output, instead of only Prime Numbers.
[EDIT] Normally I'd use an array for this, but my professor is pretty picky about everyone staying at the skill level of the class with assignments that are reviewed by my peers. It's so that the other kids will understand my code and be focused on helping me change my programming style instead of trying to figure out what an array is.
Here's my code:
This is due by 12 AM EST tomorrow. It's an online assignment. Any help would be much appreciated!
My code compiles, but every single number starting from 2 gets output, instead of only Prime Numbers.
[EDIT] Normally I'd use an array for this, but my professor is pretty picky about everyone staying at the skill level of the class with assignments that are reviewed by my peers. It's so that the other kids will understand my code and be focused on helping me change my programming style instead of trying to figure out what an array is.
Here's my code:
Code:
import javax.swing.*;
public class Assignment7PrimeNumGen
{
public static void main(String [] args)
{
int intNumberToTest;
int intPrimeCheck;
String strOutput = "";
//Loop 1: Each number is chosen 1 by 1 and incremented to 100.
for(intNumberToTest = 1; intNumberToTest <= 100; intNumberToTest++)
{
//Loop 2: The number is now tested to see if it's prime
for(intPrimeCheck = intNumberToTest - 1; intPrimeCheck > 0; intPrimeCheck--)
{
if(intNumberToTest % intPrimeCheck == 0)
{
strOutput += intNumberToTest + "\n";
break;
}
}
}
//Output
JOptionPane.showMessageDialog(null, strOutput, "Prime Numbers", JOptionPane.INFORMATION_MESSAGE);
}
}



Comment