Well I basically feel like a complete idiot right now. I tried both of your examples trying to adapt it to my code but my head is just not clear right now.
java
/**
* @(#)numbergame.java
*
* numbergame application
*
* @Kevin Ryan
* @version 1.00 2008/9/5
*/
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
public class numbergame
{
public static void main(String[] args)
{
int play, error = 0, playagain; // declares play and error which prompt the start box and whether or not there were errors
double[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
String s2 = "0", s = "0", even = "", prime = ""; // declares strings for prime, even, and the input string before and after trim
double d = 0 , squareroot, squared; // declares double for d which is the value holder, squareroot, and squared.
play = JOptionPane.showConfirmDialog (null,"Would you like to play a game?"); // prompts to start game
if (play == JOptionPane.YES_OPTION) // if the user presses yes
if (play == JOptionPane.CANCEL_OPTION)
System.exit(0);
if (play == JOptionPane.NO_OPTION)
System.exit(0);
do{
{
s = JOptionPane.showInputDialog ("Enter a number between 1 and 100\nand click ok"); // input box
s2 = s.trim(); // trims answer
}
try {
d = Double.parseDouble(s2); // tries to parse into a double
}
catch(NumberFormatException ex) // if it can't parse it
{
error = 1; // throws an error
JOptionPane.showMessageDialog(null, "You entered an invalid value.\nSystem Critical!\nShuting down...", "!!!", JOptionPane.ERROR_MESSAGE); // tells them they entered an invalid value
if (error == 1) System.exit(0); // closes program if errors == 1
}
if (d < 0 || d > 100) JOptionPane.showMessageDialog(null, "You entered a value that is out of range.\nSystem Critical!\nShuting down...", "!!!", JOptionPane.ERROR_MESSAGE); // if the number is too big
if (d < 0 || d > 100) System.exit(0); // if the number is too big
if ( ( d % 2 ) == 0) even = "even"; // if its even
if ( ( d % 2 ) != 0) even = "odd"; // if its odd
for (int scan = 0; scan < primeNums.length; scan++) // scans array of primeNums[]
{
if (d == primeNums[scan]) prime = "Prime"; // if it finds a prime
else if (d != primeNums[scan]) prime = "Composite"; // if it doesnt find a prime
}
squareroot = Math.sqrt(d); // gets the squareroot
squared = d * d; // squares it
// Displays output.
if (d >= 0 || d <= 100) JOptionPane.showMessageDialog(null, "You entered: " + d + "\n" + d + " is " + even + "\nThe squareroot of " + d + " is: " + squareroot + "\nThe square of " + d + " is " + squared + "\nThat number is a " + prime + " number");
playagain = JOptionPane.showConfirmDialog(null, "Would you like to play again?");
}
while (playagain == JOptionPane.YES_OPTION);
}
}
I realized that the code i had
CODE
if ( ( d % 2 ) != 0) prime = "Prime"; // if the number is a decimal
was taking out all odd numbers, not sure how i didn't catch that before.
I'm not even sure as to what to do at this point. I can't turn it in as is considering it doesn't even work at all now.
Gah I hate getting this frustrated it makes it even more impossible to think