#4: The Console Matters

The biggest problem is professors and textbook authors who don't want to rewritew the same tired old Pascal exercises they've been teaching for 20 years.

public class Homework1 {

  public static void main(String[] args) {
 
    System.out.println(
      "Please enter numbers, 1 to a line, -1 to quit");
    double sum = 0.0;
    while (true) {
      int i = System.readInt();
      if (i == -1) break;
      sum = sum + readInt();   
    } 
    System.out.println("The sum is " + sum);
  }

}
The right way:

public class Homework1 {

  public static void main(String[] args) {
 
    double sum = 0;
    for (int i = 0; i < args.length; i++) {
      sum = sum + Double.parseDouble(args[i]);   
    } 
    System.out.println("The sum is " + sum);
  }

}

Previous | Next | Top
Other Presentations | Cafe au Lait Home
Last Modified May 16, 1999
Copyright 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu