Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Java 10-13-2015 Java’s version of Repeat until.

Similar presentations


Presentation on theme: "AP Java 10-13-2015 Java’s version of Repeat until."— Presentation transcript:

1 AP Java 10-13-2015 Java’s version of Repeat until

2 Learning Objectives Review the while loop. Be able to write a program using the do loop.

3 class LoopExample { public static void main (String[] args ) { int a = 10; int b= 4; while (b< a) { b= b + b / 3; System.out.println(a + b); } Dry run the following

4 class LoopExample2 { public static void main (String[] args ) { int a = 0; String name = “West Salem Cross Country”; while (!name.substring(a,a+1).equals(“C”)) { a++; } System.out.println(name.substring(a)); } Dry run the following

5 Java does not have repeat..until Instead it uses do..while(condition); Referred to as a ‘do’ loop. When to use it –Repeating an unknown number of times –Occurs at least one time Examples –Checking calculations –If it makes sense to do something ‘until’ a condition is not met. –While the condition is true, it will repeat again.

6 do..while loop public class DoWhileTest { public static void main( String args[] ) { int counter = 1; // initialize counter do { System.out.printf( "%d ", counter ); counter++; } while ( counter <= 10 ); // end do...while System.out.println(); // outputs a newline } // end main } // end class DoWhileTest What does this do?

7 Do.. semantics When –When you want to repeat something an unknown number of times and at least once. Semantics –Initialize variable (Pick the number the other person needs to guess) –do Get/calculate variable (Make a guess) (Squiggly line) All the other stuff you want to repeat (Have the timer ticking, tell them if the guess is too high or too low, etc.) –while (variable != flag) (They have not guessed correctly)

8 import java.util.Scanner; public class GuessGame { public static void main( String [] args ) { Scanner input = new Scanner(System.in); int guess, counter = 0; // initialize counter do { } while ( ); // end do...while System.out.println("You guessed it in " + count + " guesses"); } Modify the following to make a guessing game.

9 Program options Write a program to test Ulam’s conjecture. –Enter any positive integer. If it is even, divide it by two. If it is odd, multiply it by three and add one. Ulam suggests that if you do this repeatedly, you will eventually get to the number 1. The program will show each number in this sequence. Yahtzee!!! Write a program that will find the number of rolls it takes to get Yahtzee ( 5 dice all with the same value). Write a program that will calculate the number of bounces it takes for a ball to return to a height of less than one foot. –The user enters the starting height, –The ball bounces to 80% of its previous height. –Show the height after each bounce and the total number of bounces. Example: –Input: 10 feet –Calculation: 10*0.80 = 8 feet 8*0.80 = 6.4 feet 6.4*0.80 = 5.12 feet 5.12*0.8 = 4.096 feet 4.096*0.8 = 3.2768 feet 3.2768*0.8 = 2.62144 feet 2.62144*0.8 = 2.097152 feet 2.097152*0.8 = 1.6777216 feet 1.6777216*0.8 = 1.3421772 feet 1.3421772*0.8 = 1.0737417 feet 1.0737417*0.8 = 0.8589933 feet Starting from 10 feet it took 11 bounces to get below 1 foot.


Download ppt "AP Java 10-13-2015 Java’s version of Repeat until."

Similar presentations


Ads by Google