public static void main(String[] args) {

    int inputNumber; // One of the integers input by the user.
    int sum; // The sum of the positive integers.
    int count; // The number of positive integers.
    double average; // The average of the positive integers.

    /* Initialize the summation and counting variables. */

    sum = 0;
    count = 0;

    /* Read and process the user's input. */

    TextIO.put("Enter your first positive integer: ");
    inputNumber = TextIO.getlnInt();

    while (inputNumber != 0) {
      sum += inputNumber; // Add inputNumber to running sum.
      count++; // Count the input by adding 1 to count.
      TextIO.put("Enter your next positive integer, or 0 to end: ");
      inputNumber = TextIO.getlnInt();
    }

    /* Display the result. */

    if (count == 0) {
      TextIO.putln("You didn't enter any data!");
    } else {
      average = ((double) sum) / count;
      TextIO.putln();
      TextIO.putln("You entered " + count + " positive integers.");
      TextIO.putf("Their average is %1.3f.\n", average);
    }
  } // end main()
Example #2
0
  public static void main(String[] args) {

    int eggs; // Number of eggs, input by user.
    int gross; // How many gross in that number of eggs?
    int aboveGross; // How many eggs are left over, above an
    //    integral number of gross?  This number
    //    can be computed as eggs % 144, and is
    //    in the range 0 to 143.  This number will
    //    be divided into dozens and extras.
    int dozens; // How many dozens in aboveGross?
    int extras; // How many eggs are left over, above integral
    //    numbers of gross and dozens?

    TextIO.put("How many eggs do you have?  ");
    eggs = TextIO.getlnInt();

    gross = eggs / 144;
    aboveGross = eggs % 144;

    dozens = aboveGross / 12;
    extras = aboveGross % 12;

    TextIO.put("Your number of eggs is ");
    TextIO.put(gross);
    TextIO.put(" gross, ");
    TextIO.put(dozens);
    TextIO.put(" dozen, and ");
    TextIO.put(extras);
    TextIO.putln();
  } // end main()
Example #3
0
 /**
  * Example output if user enters 10: Max? 1 + 3 + 5 + 7 + 9 = 25 25 = 9 + 7 + 5 + 3 + 1
  *
  * <p>Example output if user enters 11: Max? 1 + 3 + 5 + 7 + 9 + 11 = 36 36 = 11 + 9 + 7 + 5 + 3 +
  * 1
  */
 public static void main(String[] args) {
   TextIO.putln("Max?");
   int max = TextIO.getlnInt();
   int sum = 0;
   for (int i = 0; i <= max; i++) {
     if (i % 2 == 1) {
       sum = sum + i;
       if (i == max || i == max - 1) TextIO.put(i + " = ");
       else TextIO.put(i + " + ");
     }
   }
   TextIO.putln(sum);
   TextIO.put(sum + " = ");
   for (int j = max; j > 0; j--) {
     if (j % 2 == 1) {
       if (j == 1) TextIO.put(j);
       else TextIO.put(j + " + ");
     }
   }
 } // end of main method