Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Similar presentations


Presentation on theme: "Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used."— Presentation transcript:

1 Introduction to Computing Concepts Note Set 15

2 JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used to give helpful messages, errors, or warnings.

3 Example import javax.swing.JOptionPane; public class Tester { public static void main (String [] args) { JOptionPane.showMessageDialog(null, "Hello There!!!"); }

4 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

5 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); }

6 Use JOptionPane to get 5 numbers from the user then display to the screen. public static void main(String [] args) {

7 Breakout 1

8 do…while Loop do //some Java Statement while (expression); do { //some //statements //can go here } while (expression); single statement no braces multiple statements need braces

9 Two Types of Loops do { //some //statements //can go here } while (expression); VS: while(expression) { //do something //here }

10 What is Displayed int x = 1; while (x < 0) System.out.println(x); do System.out.println(x); while (x < 0);

11 What is Displayed? int c = 10; do { System.out.println(“hello”); c++; } while (c < 4);

12 What is Displayed? int v = 0; do { v++; System.out.println(v); } while (v < 4);

13 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 );

14 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

15 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; }

16 Breakout 2

17 17 For Loop Ideal for situations that require a counter Has built in expressions that initialize and update variables

18 18 For Loop for (initialization; test; update) statement; for (initialization; test; update) { statement; //Place as many here as needed }

19 19 For Loop for (initialization; test; update) statement; Used to set up counter variable

20 20 For Loop for (initialization; test; update) statement; Controls execution of loop

21 21 For Loop for (initialization; test; update) statement; Updates (increments) counter variable

22 22 For Loop for (int num=0; num<4; num++) System.out.println(num + “\t” + num*num); 1. Perform initialization of counter

23 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 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 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


Download ppt "Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used."

Similar presentations


Ads by Google