Do…while loops in Java, plus a look at variable scoping and multi-line comments. In this tutorial I tackle a task that’s often assigned to beginners on Java courses; looping until the user enters some particular input.
When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.
Code for this tutorial:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/*
System.out.println("Enter a number: ");
int value = scanner.nextInt();
while(value != 5) {
System.out.println("Enter a number: ");
value = scanner.nextInt();
}
*/
int value = 0;
do {
System.out.println("Enter a number: ");
value = scanner.nextInt();
}
while(value != 5);
System.out.println("Got 5!");
}
}
Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.
All pages on this site are copyright © 2013 John W. Purcell