Saturday, February 2, 2013
Java Programming, Do While, If, Miles To Kilometer , Part 2
Use do while loop to repeat the program, to exit user enters - 1.0 And also if clause
Code below :
// Miles to kilometer example
// In part two we use do while to repeat the program
// and also if and else
// Use scanner class
import java.util.Scanner;
public class MilesKilometer2 {
public static void main(String[] args){
double miles ;
// use constant
final double MILES_TO_KILOMETER = 1.609 ;
//Initialize scanner class
Scanner newscan = new Scanner(System.in);
// To repeat use do while loop
do {
System.out.println("Miles to kilometer program \n Enter -1.0 to terminate ");
System.out.print("Enter miles value : ");
// Read miles value
miles = newscan.nextDouble();
// Print out the value
// if miles not equal to -1 print the kilometer value
if( miles != -1.0 )
{
System.out.println("Kilometer value is : " + (miles * MILES_TO_KILOMETER ) + "\n");
}
// say good bye
else { System.out.println("Thank you, bye");}
} while(miles != -1.0); // loop will terminate if miles value is -1.0
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment