Improved Prompt Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The complete program is given below.

Improved Prompt Program

You could have answered the question by mechanically writing in a statement to increment count, as the documentation suggests. Hopefully you understand the larger context of what is going on. Often in programming, several variables have to be kept coordinated like this. It is a juggling act.

    int count = 0;   // number of integers read in

    // get the first value
    System.out.println( "Enter first integer (enter 0 to quit):" );
    value = scan.nextInt();

    while ( value != 0 )    
    {
      //add value to sum
      sum = sum + value;

      //increment the count
      count = count + 1;

      //get the next value from the user
      System.out.println( "Enter the " + 
          (count+1) + "th integer (enter 0 to quit):" );
      value = scan.nextInt();
    }

    System.out.println( "Sum of the "  + count + " integers: " + sum );
  }
}

Let us work on correcting the grammar in the prompt. We will use nested if-else statements inside the loop body. This will get somewhat complicated.

QUESTION 7:

What suffix goes with each integer? E.g. 1 gets "st" to make 1st.

  • 1
  • 2
  • 3
  • 4 and higher.