Difference between revisions of "Personal Greeting"
Line 7: | Line 7: | ||
*prompt the user for their age | *prompt the user for their age | ||
*output a greeting including both the name and age gathered from input | *output a greeting including both the name and age gathered from input | ||
+ | <br/> | ||
+ | Make sure to use JOptionPane for your input and output | ||
<br/> | <br/> | ||
Example: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today." | Example: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today." | ||
Line 26: | Line 28: | ||
− | + | JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today."); | |
} | } | ||
Line 33: | Line 35: | ||
− | |SideSectionTitle= | + | |SideSectionTitle=Personal Greeting |
|SideSection= | |SideSection= | ||
Line 46: | Line 48: | ||
<pre> import javax.swing.*;</pre><br/> | <pre> import javax.swing.*;</pre><br/> | ||
− | Define your variables, we will need | + | Define your variables, we will need a String for your name and an int for age. |
− | + | ||
<pre> | <pre> | ||
− | + | String name; | |
− | + | int age; | |
</pre><br/> | </pre><br/> | ||
− | Next start by capturing the user input using JOptionPane. We will need to use Integer.parseInt to cast the String result to an integer for | + | Next start by capturing the user input using JOptionPane. We will need to use Integer.parseInt to cast the String result to an integer for your age. |
<pre> | <pre> | ||
− | + | name = JOptionPane.showInputDialog("Please enter your name") ; | |
− | + | age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age")); | |
</pre><br/> | </pre><br/> | ||
− | Now | + | Now output your message using JOptionPage.showMessageDialog |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
<pre> | <pre> | ||
− | + | JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today."); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</pre> | </pre> | ||
− | |||
− |
Revision as of 12:40, 6 April 2010
{{1010PrAD
|ProblemName=Personal Greeting
|Problem=Write a Java program PersonalGreeting, that asks a series of questions and returns a response.
The program should:
- prompt the user for their name
- prompt the user for their age
- output a greeting including both the name and age gathered from input
Make sure to use JOptionPane for your input and output
Example: User inputs "Elmo" and "10" would result in something like "Welcome to COMP 1010 Elmo, you are 10 years old today."
|SolutionCode=
import javax.swing.*; public class PersonalGreeting { public static void main(String[] args) { String name; int age; name = JOptionPane.showInputDialog("Please enter your name") ; age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age")); JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today."); } }
|SideSectionTitle=Personal Greeting
|SideSection=
Taken from http://www.flickr.com/photos/daniello/565304023/
An image or By Students section
|Solution=Start by importing the swing java package.
import javax.swing.*;
Define your variables, we will need a String for your name and an int for age.
String name; int age;
Next start by capturing the user input using JOptionPane. We will need to use Integer.parseInt to cast the String result to an integer for your age.
name = JOptionPane.showInputDialog("Please enter your name") ; age = Integer.parseInt(JOptionPane.showInputDialog("Please enter your age"));
Now output your message using JOptionPage.showMessageDialog
JOptionPane.showMessageDialog(null, "Welcome to COMP1010 " + name + ", you are " + age + " years old today.");