2008年12月11日 星期四

simple java(十一)---application(8)

The factorial of a positive integer N is equal to the product of 1 to N. Write aprogram to ask the user to enter a positive integer N and then compute N! using awhile loop to repeatedly multiplying numbers 1 to N to a value.

solution:用while loop

import javax.swing.*;

public class range {

public static void main(String args[]) {

int i=1,number,result1=1;

String input;

input=JOptionPane.showInputDialog("enter a positive integer");

number=Integer.parseInt(input);

while(i<=number){

result1=result1*i;

System.out.println( "i = " + i + ", result1 = " + result1 );

i++;

}
System.exit(0);}}
********************************
Write an application that displays a menu in Dialog box, reads the choice fromthe user and displays it. The application reads the choice again until the userselects “5 Exit” to exit the system.
solution:
import javax.swing.JOptionPane;
public class Lab73a {
public static void main ( String args [] ) {
int n;
do {
n = Integer.parseInt ( JOptionPane.showInputDialog ( "Select one of the following (1 - 5):\n1 Apple\n2 Bomb\n3 Cat\n4 Dog\n5 Exit" ));
if ( n == 1)
JOptionPane.showMessageDialog ( null, "You select - 1 Apple\nContinuous ...");
else if ( n == 2 )
JOptionPane.showMessageDialog ( null, "You select - 2 Bomb\nContinuous ...");
else if ( n == 3 )
JOptionPane.showMessageDialog ( null, "You select - 3 Cat\nContinuous ...");
else if ( n == 4 )
JOptionPane.showMessageDialog ( null, "You select - 4 Dog\nContinuous ...");
else if ( n == 5)
System.exit (0);
elseJOptionPane.showMessageDialog ( null, "Please select an integer 1 to 5.\nContinuous ...");}
while ( n <. 5 );}}

沒有留言: