Difference between revisions of "Personal Greeting"
From CompSciWiki
Line 3: | Line 3: | ||
|ProblemName=Personal Greeting | |ProblemName=Personal Greeting | ||
− | |Problem=Write a Java program PersonalGreeting, that asks a series of questions and returns a response.<br/>For all input and output please use [[Input/Output_using_JOptionPane|JOptionPane]] to help review the first chapter's material.<br/>Your program should do the following: | + | |Problem=Write a Java program PersonalGreeting, that asks a series of questions and returns a response.<br/>For all input and output please use [[Input/Output_using_JOptionPane|JOptionPane]] to help review the first chapter's material.<br/><br/>Your program should do the following: |
*prompt the user for their name ([[Strings|String]]) | *prompt the user for their name ([[Strings|String]]) | ||
*prompt the user for their age ([[Common_Primitive_Variables#Primitive_Type_int|int]]) | *prompt the user for their age ([[Common_Primitive_Variables#Primitive_Type_int|int]]) |
Revision as of 21:43, 6 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. 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."); | ||
Code |