Difference between revisions of "Personal Greeting"
(EDIT: (Problem, Example sentence) reworded) |
(EDIT: (SideSectionTitle) changed "pleasent" to "pleasant") |
||
Line 43: | Line 43: | ||
− | |SideSectionTitle=Personal Greeting<br/>This is more | + | |SideSectionTitle=Personal Greeting<br/>This is more pleasant looking in person |
|SideSection= | |SideSection= |
Revision as of 14:21, 8 April 2010
Back to the Program-A-Day homepage
ProblemWrite a Java program PersonalGreeting, that asks a series of questions and returns a response.
|
Personal Greeting
| |
---|---|---|
SolutionStart by importing the swing java package. This is in order to call the JOptionPane class. import javax.swing.*; Define your variables, we will need a String for your name and an int for age. //local variables String name; int age; Next start by capturing the user input using JOptionPane.showInputDialog. We will need to use Integer.parseInt to cast the string result to an integer for your age. //input name = JOptionPane.showInputDialog("Please enter your name") ; age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age")); Now output your message using JOptionPage.showMessageDialog. Make sure first parameter is null. Don't forget to use the + operator when appending strings also sometimes referred to as concatenation. //output JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today."); | ||
Code |