Java Course 6: while and do While Loops

Counting Down

int count = 10;
while (count>0)
{
    System.out.println(count);
    count = count - 1;
}
System.out.println("Lift off!");

Counting Objects

String colours = "red blue red blue blue blue blue red blue red blue "
		 + "red blue blue red blue blue blue red blue red red "
		 + "blue blue red red red red blue red blue red blue blue";


Scanner scan = new Scanner(colours);
int redcount = 0;

while (scan.hasNext()) {
    String s = scan.next();
    if (s.equals("red")) {
	redcount = redcount + 1;
    }
}
System.out.println("Number of reds " + redcount);

A Basic Question Loop

Scanner scan = new Scanner(System.in);
String answer;

do
{
    System.out.println("What's the capital of France?");
    answer = scan.nextLine(); 

}while (!answer.equals("Paris"));
 System.out.println("Correct!");

A Question Loop with a Count

Scanner scan = new Scanner(System.in);
String answer;
int count = 0;

do
{
    System.out.println("What's the capital of France?");
    answer = scan.nextLine();
    count = count + 1;

}while (!answer.equals("Paris"));
 System.out.println("Correct!");
 System.out.println("It took you " + count + " guesses");

A Question Loop with a Flag

Scanner scan = new Scanner(System.in);
int answer;
boolean isNotDone = true;

do
{
    System.out.println("Enter an Integer, 0 to end.");
    answer = scan.nextInt();
    System.out.println(answer + " squared = " + answer * answer);
    if (answer == 0)
    {
	isNotDone = false;
    }

}while (isNotDone);
 System.out.println("Done!");

A Question Loop with a Count and a Flag

Scanner scan = new Scanner(System.in);
String answer;
boolean isCorrect = false;
int count = 0;
do
{
    System.out.println("Enter the password");
    answer = scan.nextLine();
    if (answer.equals("Java"))
    {
	isCorrect = true;
    }
    count = count + 1;
 }while(!isCorrect & count <3 );


if (isCorrect)
{
    System.out.println("Correct password");
}
else
{
    System.out.println("Incorrect password");

Exercise

while Questions

  1. Use a while loop to print out the integers from 1 to 10
  2. Use a while loop to print out the even numbers from 2 to 20
  3. Use a while loop to print out the four times table
  4. Use a while loop to print out the five times table in the form “n x 5 = 5n”
  5. Use a while loop to print out the sequence 7,10,13,16,19,22,25
  6. Use a while loop to print out the sequence -0.3, -0.2, -0.1, 0, 0.1, 0.2
  7. Use a while loop to print out the sequence 64, 32, 16, 8, 4, 2, 1
  8. Use a while loop and Scanner to count how many heads and tails there are in the String below. (nb. you should carefully copy and paste the String into your program)

String coins = “head head tail head tail head head head tail head tail head tail head head head tail “+ “head tail head tail head tail head tail head tail tail tail head head head tail “+ “tail head tail head tail tail head head head tail tail tail head tail head head head tail “+ “head tail tail head tail head head head tail head tail head tail head head head head “+ “head tail head tail head tail head head head tail tail head tail head tail head head head head”;

  1. Write a code to find the number of times the word “never” appears in the following poem: I never saw a purple cow, I never hope to see one, but I can tell you anyhow, I’d rather see than be one.”
  2. Now search the Internet for the text to the poem “McCavity the Mystery Cat” Write code to count the number of times the word McCavity appears.

do while Questions

  1. Make the computer keep asking “Are we there yet?” until the answer yes is input.
  2. Ask the user the to guess a number between one and ten. Hard code the number 4 as an answer. The computer will print out how many guesses the user takes before they get the correct answer.
  3. Modify program 2 so that the computer chooses a random number as the answer.
  4. Write a higher lower game. Modify program 3. The computer thinks of a number between 1 and 100. The user tries to guess the number, and the computer tells the user if their answer is higher or lower. Whe then user guesses correctly, the computer congratulates the user and tells them how many guesses it took them.
  5. Wrtie a password checker. The user cannot proceed until they enter the correct password.
  6. Modify your password checker so that the user sees the message “Locked Out” if they fail to guess the password in three guesses.

Extension

The skeleton code below is intended to guess the number the end user is thinking of. The end user can enter yes if the computer guesses the answer, higher if the number is higher and lower otherwise. Copy the code into the IDE and complete it.

Scanner scan = new Scanner(System.in);
double num = Math.random()*100;
int ran = (int)num+1;
int guess = 50;
int high =100;
int low= 0;
String input = "";
boolean isCorrect = false;

do
{
    System.out.println("Is the number " + guess + "?");
    input = scan.nextLine();
    if(input.equals("yes"))
    {
	isCorrect = true;
    }
    else if(input.equals("higher") )
    {
	low = guess;
	guess = low + (int)(high-low)/2;
    }


}while(!isCorrect);

Leave a Comment