import java.util.Scanner;
import java.io.IOException;
/**
PromptForInput02 is a second simple example of prompting for input
in the style of "old school intro courses"---one that computes the
area of a triangle.
I don't advocate this style of programming once students are beyond
"training-wheels level examples"--there are better alternatives such
as getting input from command line args, or reading from files-- I
nevertheless feel obliged to provide examples of how to do it.
@author Phill Conrad
@version 2010.12.30
@see Java Cookbook 2nd edition, Recipe 10.1 (on campus)
@see (off campus)
*/
public class promptForInput02 {
/** A main method to illustrate the "training wheels programming"
approach to computing area of a triangle.
This method throws IOException
to keep the example
relatively simple. The alternative is a try/catch block
@param args value is ignored
*/
public static void main (String [] args) throws IOException {
// One way to do this is to use the Scanner class.
Scanner sc = new Scanner(System.in); // Requires JDK 1.5
System.out.println("I'll help you compute the area of a triangle!");
System.out.print("Please enter base: "); // like a printf (or cout << "prompt")
double base = sc.nextDouble( ); // like a scanf (or cin >> base)
System.out.print("Please enter height: "); // like a printf (or cout << "prompt")
double height = sc.nextDouble( ); // like a scanf (or cin >> height)
// compute and print answer
System.out.println("The area of the triangle is: " + 0.5 * base * height);
} // main
} // class promptForInput02