Title: Loops
1Loops
2(No Transcript)
3public class CatchBad public static void
main(String args) int number
GetData.getInteger("Enter a number from 1 to
10") if (number lt 1 number gt 10)
System.out.println("ERROR - this is a bad
number") System.out.println("Only enter
numbers from 1 to 10") number
GetData.getInteger("Enter a number from 1 to
10") System.out.println("The number
you entered was "number)
Enter a number from 1 to 10 15 ERROR -
this is a bad number Only enter numbers from 1 to
10 Enter a number from 1 to 10 -3 The number you
entered was -3
4(No Transcript)
5public class CatchBad public static void
main(String args) int number
GetData.getInteger("Enter a number from 1 to
10") while (number lt 1 number gt 10)
System.out.println("ERROR - this is a
bad number") System.out.println("Only
enter numbers from 1 to 10") number
GetData.getInteger("Enter a number from 1 to
10") System.out.println("The number
you entered was "number)
Enter a number from 1 to 10 15 ERROR -
this is a bad number Only enter numbers from 1 to
10 Enter a number from 1 to 10 -3 ERROR -
this is a bad number Only enter numbers from 1 to
10 Enter a number from 1 to 10 77 ERROR -
this is a bad number Only enter numbers from 1 to
10 Enter a number from 1 to 10 5 The number you
entered was 5
6 int number GetData.getInteger("Enter a number
from 1 to 10") while (number lt 1 number
gt 10) System.out.println("ERROR -
this is a bad number") System.out.println("
Only enter numbers from 1 to 10") number
GetData.getInteger("Enter a number from 1 to
10") int number GetData.integerRange(1
, 10, "Enter a number from 1 to 10")
7(No Transcript)
8public class CountDown public static void
main(String args) int number
GetData.getInteger("Enter a number from 1 to
20") while (number lt 1 number gt 20)
System.out.println("ERROR - this is a
bad number") System.out.println("Only
enter numbers from 1 to 20") number
GetData.getInteger("Enter a number from 1 to
20") while (number gt 0)
System.out.println(number) number number
-1
9public class CountDown public static void
main(String args) int number
GetData.integerRange(1, 20, "Enter a number from
1 to 20") while (number gt 0)
System.out.println(number--)
Enter a number from 1 to 20 23 Error - 1 to 20
only! Try again. Enter a number from 1 to 20
Hello Hello is not valid. Enter a number from 1
to 20 7 7 6 5 4 3 2 1
10(No Transcript)
11public class CountDown public static void
main(String args) int number
GetData.integerRange(1, 20, "Enter a number from
1 to 20") do System.out.println(numb
er--) while (number gt 0)
12Exercise 1
- Write a program to
- Read in test scores until -1 is entered.
- Only allow test scores from 0 to 100.
- Print out the average test score for the class.
- Display the number of students in the class.
- Round to 1 decimal.
- More examples on next page.
Example 1 Enter test score or -1 to exit
90 Enter test score or -1 to exit 96 Enter test
score or -1 to exit 93 Enter test score or -1 to
exit 81 Enter test score or -1 to exit 75 Enter
test score or -1 to exit 87 Enter test score or
-1 to exit -1 The average of 6 students is
87.0 Thanks for using the average program.
13 Exercise 1 Examples continued
Example 2 Enter test score or -1 to exit
52 Enter test score or -1 to exit 83 Enter test
score or -1 to exit 105 Error - -1 to 100
only! Try again. Enter test score or -1 to exit
75 Enter test score or -1 to exit 93 Enter test
score or -1 to exit 100 Enter test score or -1
to exit -1 The average of 5 students is
80.6 Thanks for using the average
program. Example 3 Enter test score or -1 to
exit -1 There were no students. Thanks for
using the average program.
14Example of using a do-while to control a program.
Here is an example of the guessing game program.
Notice that we can play it over and over without
restarting it.
import java.util. public class GuessNumber2
public static void main(String args)
Random generator new Random() int
humanGuess 0 int computerNumber 0
int difference 0 String yesOrNo ""
15continued
do computerNumber generator.nextInt(10)
1 humanGuess GetData.integerRange(1,10,
"Enter guess") difference
Math.abs(computerNumber - humanGuess)
System.out.println("\n\nYOUR GUESS "
humanGuess) System.out.println("COMPUTER'S
NUMBER " computerNumber)
System.out.println("DIFFERENCE " difference)
System.out.print("RATING ") switch
(difference) case 0 System.out.println(
"Excellent")break case 1
System.out.println("Very Good")break
case 2 System.out.println("Good")break
case 3 System.out.println("Fair")break
case 4 System.out.println("Average")break
case 5 System.out.println("Below
Average")break case 6
System.out.println("Bad")break case 7
System.out.println("Very Bad")break
case 8 System.out.println("Stupid")break
case 9 System.out.println("Idiot")break
default System.out.println("ERROR")break
yesOrNoGetData.getString("Do you want
to play again? (Yes/No)") while
(yesOrNo.compareToIgnoreCase("Yes") 0)
16(No Transcript)
17(No Transcript)
18public class ForLoopExample public static
void main(String args) int number
for (number 1 number lt 10 number )
System.out.println(number)
19public class ForLoopExample public static
void main(String args) int number
GetData.getInteger("Enter a number") for
(int x number x gt 0 x--)
System.out.println(x)
20public class ForLoopExample public static
void main(String args) int number
GetData.getInteger("Enter a number") for
(int x 0 x lt number x2)
System.out.println(x)
21public class ForLoopExample public static
void main(String args) float number
GetData.getFloat("Enter a number") for
(float x number x lt 10000000 x2)
System.out.println(x)
2.175 4.35 8.7 17.4 34.8 69.6 139.2 278.4 556.8 11
13.6 2227.2 4454.4 8908.8 17817.6 35635.2 71270.4
142540.8 285081.6 570163.2 1140326.4 2280652.8 456
1305.5 9122611.0
22public class ForLoopExample public static
void main(String args) for (char x
'A' x lt 'Z' x) if (x ! 'Z')
System.out.print(x ", ") else
System.out.println(x ".")
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P,
Q, R, S, T, U, V, W, X, Y, Z.
23Exercise 2
Ask a user for a number from 1 to 20. Make sure
that number is from one to 20 using the
integerRange() method of GetData. Print out all
of the numbers from 1 to whatever number they
entered. See the following example Enter a
number from 1 to 20 15 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15. Notice that there are
commas and spaces between numbers, and a period
after the last number. You must use a FOR loop
to do this