Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows...
-
Upload
claude-welch -
Category
Documents
-
view
214 -
download
0
Transcript of Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows...
Introduction to Computing ConceptsNote Set 15
JOptionPane.showMessageDialog•Message Dialog•Allows you to give a brief message to the user
•Can be used to give helpful messages, errors, or warnings.
Example
import javax.swing.JOptionPane;
public class Tester {
public static void main (String [] args) {
JOptionPane.showMessageDialog(null, "Hello There!!!");
}
}
JOptionPane.showInputDialog•Pops up a dialog and allows the user to enter a string•Method returns a string
•Can be used to ask for strings or numbers.▫Must convert string version of number to numerical
type
Example
import javax.swing.JOptionPane;
public class Tester { public static void main (String [] args) { String data; data = JOptionPane.showInputDialog(null,
"Please input your name."); System.out.println("Hello“ + data); }}
•Use JOptionPane to get 5 numbers from the user then display to the screen.
public static void main(String [] args) {
Breakout 1
do…while Loop
do //some Java Statementwhile (expression);
do { //some //statements //can go here} while (expression);
single statementno braces
multiple statementsneed braces
Two Types of Loops
do { //some //statements //can go here} while (expression);
VS:
while(expression) { //do something //here}
What is Displayed
int x = 1;while (x < 0) System.out.println(x);
do System.out.println(x);while (x < 0);
What is Displayed?
int c = 10;do { System.out.println(“hello”); c++;} while (c < 4);
What is Displayed?
int v = 0;do { v++; System.out.println(v); } while (v < 4);
What is Displayed?int count = 0;int funny = 1;int serious = 0;int limit = 4;do { funny ++; serious = serious + 2; count = count + 1;} while (count < limit);System.out.println(funny + “ “ + serious + “ “ + count );
QuickCode•Write a snippet of code that asks the user to enter
integers until the sum of the numbers entered is greater than 300. Use a do…while loop
Nested Loops• It is possible to have one looping structure inside
another
int first = 0;int second = 0;while (first < 3) { do { System.out.print(“*”); //doesn’t go to next line second += 1; }while (second < 4); System.out.println(); first += 1; second = 0;}
Breakout 2
17
For Loop
• Ideal for situations that require a counter
•Has built in expressions that initialize and update variables
18
For Loop
for (initialization; test; update)
statement;
for (initialization; test; update){
statement;statement;//Place as many here as needed
}
19
For Loop
for (initialization; test; update)
statement;
Used to set up counter variable
20
For Loop
for (initialization; test; update)
statement;
Controls execution of loop
21
For Loop
for (initialization; test; update)
statement;
Updates (increments) counter variable
22
For Loop
for (int num=0; num<4; num++)
System.out.println(num + “\t” + num*num);
1. Perform initialization of counter
23
for (int num=0; num<4; num++)
System.out.println(num + “\t” + num*num);
For Loop
1. Perform initialization of counter
2. Evaluate test
• if num < 4, go to step 3
• else terminate loop
24
for (int num=0; num<4; num++)
System.out.println(num + “\t” + num*num);
For Loop
1. Perform initialization of counter
2. Evaluate test
3. Execute statement
25
for (int num=0; num<4; num++)
System.out.println(num + “\t” + num*num);
For Loop
1. Perform initialization of counter
2. Evaluate test
3. Execute statement
4. Perform update – go back to step 2