Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-07-2007, 04:51 PM   #1
Beta Member
 
Join Date: Jun 2007
Posts: 4
Default JOptionPane.showMessageDialog does not display! HELP NEEDED

Ok this is a simple program and should work..... I cannot figure out why it doesn't.

The user inputs a number, a for loop is initiated to calculate the factorial of the number, and prints the value to the screen.

IF the number is negative a message is displayed that the user must enter a positive number (MY MESSAGE DISPLAY DOESN'T WORK IF A NEGATIVE NUMBER IS ENTERED.... it just sits there and nothing happens).

Any help is appreciated.

import java.util.*;
import javax.swing.*;
public class Factorial {
public static void main(String[] args) {

int n; // variable to store number for which the factorial is to be calculated
int counter; // loop counter
int factorial; // factorial value

Scanner scan = new Scanner(System.in);

System.out.println(
"This program asks the user for a positive integer number,"
+ "\ncomputes the factorial value, and displays the answer."
+ "\nPlease enter a positive integer: ");
n = scan.nextInt();

if (n <= 0)
{
JOptionPane.showMessageDialog(null, "You must enter a positive number.");
}

factorial = 1;
for (counter = 1; counter <= n; counter++)
{
factorial *=counter;
}

System.out.println("The factorial of " + n +" is " +factorial);

}// end method main
}// end class Factorial
__________________

magic507 is offline   Reply With Quote
Old 06-20-2007, 01:56 PM   #2
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: JOptionPane.showMessageDialog does not display! HELP NEEDED

Well, I ran your program, and it seems to work just fine. I did make a couple changes, see if this helps you at all.
Code:
import java.util.*;
import javax.swing.*;
public class Factorial {
 static Factorial f;
public static void main(String[] args) {
int n = -1; // variable to store number for which the factorial is to be calculated
int factorial; // factorial value
Scanner scan = new Scanner(System.in);
f = new Factorial();
System.out.println(
"This program asks the user for a positive integer number,"
+ "\ncomputes the factorial value, and displays the answer."
+ "\nPlease enter a positive integer: ");
do {
n = scan.nextInt();
if (n <= 0)
{
JOptionPane.showMessageDialog(null, "You must enter a positive number.");
System.out.printf("\n\t%s","Please enter a positive integer:");
} else {
 f.compute(n, 1);
}
} while(n < 0);
}// end method main
  public void compute(int number, int fact) {
   int counter;
   fact = 1;
   for (counter = 1; counter <= number; counter++)
   {
   fact *=counter;
   }
System.out.println("The factorial of " + number +" is " +fact);
}
public Factorial() {
}
}// end class Factorial
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 12:06 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0