Tuesday, October 03, 2006

Get Input Example




// Step #1
import javax.swing.*;
public class InputExample {
public static void main(String[] args) {
// Step #2 : Declare a variable to store the input
String inputStr;
// Step #3: Prompt the user
inputStr = JOptionPane.showInputDialog(null,
"How old are you now?");
// Step #4: Convert String to int
int num = Integer.parseInt(inputStr);
// Step #5: Display results
int result = num+10;
JOptionPane.showMessageDialog(null,
"You will be " + result +
"years old in 10 years time");

} //end main
} //end class

No comments: