Home > Software engineering >  A Java program with a loop that allows the user to enter a series of integers, then displays the sma
A Java program with a loop that allows the user to enter a series of integers, then displays the sma

Time:03-19

I've got an assignment that requires me to use a loop in a program that asks the user to enter a series of integers, then displays the smallest and largest numbers AND gives an average. I'm able to write the code that allows the user to enter however many integers they like, then displays the smallest and largest number entered. What stumps me is calculating the average based on their input. Can anyone help? I'm sorry if my code is a little janky. This is my first CS course and I'm by no means an expert.

import javax.swing.JOptionPane;
import java.io.*;

public class LargestSmallest 
{
    public static void main(String[] args)    
    
    {
        int number, largestNumber, smallestNumber, amountOfNumbers;
        double sum, average;
        String inputString;

        inputString = JOptionPane.showInputDialog("Enter an integer, or enter -99 to stop.");
        number = Integer.parseInt(inputString);

        largestNumber = number;
        smallestNumber = number;

        sum = 0;

        for (amountOfNumbers = 1; number != -99; amountOfNumbers  )
            {
                inputString = JOptionPane.showInputDialog("Enter an integer, or enter -99 to stop.");
                number = Integer.parseInt(inputString);
                

                if (number == -99)
                    break;
                if (number > largestNumber)
                    largestNumber = number;
                if (number < smallestNumber)
                    smallestNumber = number;  
                
                sum  = number;
            }

            average = sum / amountOfNumbers;

        JOptionPane.showMessageDialog(null, "The smallest number is: "   smallestNumber   ".");

        JOptionPane.showMessageDialog(null, "The largest number is: "   largestNumber   ".");

        JOptionPane.showMessageDialog(null, "The average off all numbers is: "   average   ".");


    }
}

CodePudding user response:

The problem is that you do an extra

inputString = JOptionPane.showInputDialog("Enter an integer, or enter -99 to stop.");
    number = Integer.parseInt(inputString);

at the beginning. You don't count that in a sum. That's why you get unexpected results.

The fix would be:

  1. replace the declarations line with:

    int number = 0, largestNumber, smallestNumber, amountOfNumbers;

  2. Remove

    inputString = JOptionPane.showInputDialog("Enter an integer, or enter -99 to stop."); number = Integer.parseInt(inputString); That go before the loop

  3. Replace for (amountOfNumbers = 0 with for (amountOfNumbers = 1

CodePudding user response:

This is my first CS course

Then allow me to show you a different way to do your assignment.

  • Don't use JOptionPane to get input from the user. Use a Scanner instead.
  • Rather than use a for loop, use a do-while loop.
  • Usually you declare variables when you need to use them so no need to declare all the variables at the start of the method. However, be aware of variable scope.

(Notes after the code.)

import java.util.Scanner;

public class LargestSmallest {

    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int largestNumber = Integer.MIN_VALUE;
        int smallestNumber = Integer.MAX_VALUE;
        int number;
        double sum = 0;
        int amountOfNumbers = 0;
        do {
            System.out.print("Enter an integer, or enter -99 to stop: ");
            number = stdin.nextInt();
            if (number == -99) {
                break;
            }
            if (number > largestNumber) {
                largestNumber = number;
            }
            if (number < smallestNumber) {
                smallestNumber = number;  
            }
            sum  = number;
            amountOfNumbers  ;
        } while (number != -99);

        if (amountOfNumbers > 0) {
            double average = sum / amountOfNumbers;
            System.out.printf("The smallest number is: %d.%n", smallestNumber);
            System.out.printf("The largest number is: %d.%n", largestNumber);
            System.out.printf("The average of all numbers is: %.4f.%n", average);
        }
    }
}
  • largestNumber is initialized to the smallest possible number so that it will be assigned the first entered number which must be larger than largestNumber.
  • Similarly, smallestNumber is initialized to the largest possible number.
  • If the first value entered is -99 then amountOfNumbers is zero and dividing by zero throws ArithmeticException (but maybe you haven't learned about exceptions yet). Hence, after the do-while loop, there is a check to see whether at least one number (that isn't -99) was entered.
  • You don't need to use printf to display the results. I'm just showing you that option.
  • Related