Difference between revisions of "Personal Greeting"
(EDIT: (SideSectionTitle) changed "pleasent" to "pleasant") |
|||
(8 intermediate revisions by 4 users not shown) | |||
Line 9: | Line 9: | ||
<br/> | <br/> | ||
This program will cover the following topics: | This program will cover the following topics: | ||
− | *[[Input | + | *[[Input using Scanner|Scanner]] |
*[[Comments|Commnets]] | *[[Comments|Commnets]] | ||
*[[Strings|String]] | *[[Strings|String]] | ||
Line 16: | Line 16: | ||
<br/> | <br/> | ||
Example: If a user inputs "Elmo" for their name and "10" for their age, then the result would look something like "Welcome to COMP 1010 Elmo, you are 10 years old today." | Example: If a user inputs "Elmo" for their name and "10" for their age, then the result would look something like "Welcome to COMP 1010 Elmo, you are 10 years old today." | ||
+ | |||
+ | |||
+ | |SideSectionTitle=Mid-term Review | ||
+ | |||
+ | |SideSection= | ||
+ | [[Image:Wiki_trays01.jpg|center]]<BR> | ||
+ | |||
+ | |Solution=Start by importing the swing java package. We need this to make calls to the JOptionPane class. | ||
+ | |||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | import javax.swing.*; | ||
+ | import java.util.Scanner; | ||
+ | }} | ||
+ | |||
+ | |||
+ | Define your variables, we will need a [[Strings|String]] for your name and an [[Common_Primitive_Variables#Primitive_Type_int|int]] for age and [[Input using Scanner|Scanner]] for input. | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | String name; // storage for the name | ||
+ | int age; // storage for the age | ||
+ | Scanner input = new Scanner(System.in); | ||
+ | }} | ||
+ | |||
+ | |||
+ | {{note}} To avoid confusion, we will assume users will enter valid input. An unfortunate disadvantage of .nextInt() is that if the user enters non numeric values the program will throw an error. You may wish to make the program more robust if you like. In a future computer science course you will learn how to handle such situations. | ||
+ | |||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | System.out.print("Please enter your name: "); | ||
+ | name = input.next(); | ||
+ | System.out.print("Please enter your age: "); | ||
+ | age = input.nextInt(); | ||
+ | }} | ||
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Please enter your name: John | ||
+ | Please enter your age: 19 | ||
+ | }} | ||
+ | |||
+ | |||
+ | Now output your message . | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | System.out.println("Welcome to COMP1010 " + name + ", you are " + age + " years old today."); | ||
+ | }} | ||
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Welcome to COMP1010 John, you are 19 years old today. | ||
+ | }} | ||
|SolutionCode= | |SolutionCode= | ||
− | |||
import javax.swing.*; | import javax.swing.*; | ||
Line 40: | Line 89: | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} |
Latest revision as of 15:15, 3 December 2011
Back to the Program-A-Day homepage
ProblemWrite a Java program PersonalGreeting, that asks a series of questions and returns a response.
|
Mid-term Review | |
---|---|---|
SolutionStart by importing the swing java package. We need this to make calls to the JOptionPane class. import javax.swing.*; import java.util.Scanner;
String name; // storage for the name int age; // storage for the age Scanner input = new Scanner(System.in);
System.out.print("Please enter your name: "); name = input.next(); System.out.print("Please enter your age: "); age = input.nextInt(); Please enter your name: John Please enter your age: 19
System.out.println("Welcome to COMP1010 " + name + ", you are " + age + " years old today."); Welcome to COMP1010 John, you are 19 years old today. | ||
Code |