简体   繁体   中英

Store user input in array multiple times

I'm working on a project which...

Allows the user to input 4 numbers that are then stored in an array for later use. I also want every time the user decided to continue the program, it creates a new array which can be compared to later to get the highest average, highest, and lowest values.

The code is not done and I know there are some things that still need some work. I just provided the whole code for reference.

I'm just looking for some direction on the arrays part.

*I believe I am supposed to be using a 2-D array but I'm confused on where to start. If I need to explain more please let me know. (I included as many comments in my code just in case.)

I tried converting the inputDigit(); method to accept a 2-D array but can't figure it out.

If this question has been answered before please redirect me to the appropriate link.

Thank you!

package littleproject;

import java.util.InputMismatchException;
import java.util.Scanner;

public class littleProject {

public static void main(String[] args) {
    // Scanner designed to take user input
    Scanner input = new Scanner(System.in);
    // yesOrNo String keeps while loop running
    String yesOrNo = "y";
    while (yesOrNo.equalsIgnoreCase("y")) {

        double[][] arrayStorage = inputDigit(input, "Enter a number: ");

        System.out.println();

        displayCurrentCycle();
        System.out.println();

        yesOrNo = askToContinue(input);
        System.out.println();

        displayAll();
        System.out.println();

            if (yesOrNo.equalsIgnoreCase("y") || yesOrNo.equalsIgnoreCase("n")) {
                System.out.println("You have exited the program."
                    + " \nThank you for your time.");
            }
        }
    }

// This method gets doubles and stores then in a 4 spaced array
public static double[][] inputDigit(Scanner input, String prompt) {
    // Creates a 4 spaced array
    double array[][] = new double[arrayNum][4];

    for (int counterWhole = 0; counterWhole < array.length; counterWhole++){
        // For loop that stores each input by user
        for (int counter = 0; counter < array.length; counter++) {
            System.out.print(prompt);

            // Try/catch that executes max and min restriction and catches
            // a InputMismatchException while returning the array
            try {
                array[counter] = input.nextDouble();
                if (array[counter] <= 1000){
                    System.out.println("Next...");
                } else if (array[counter] >= -100){
                    System.out.println("Next...");
                } else {
                    System.out.println("Error!\nEnter a number greater or equal to -100 and"
                            + "less or equal to 1000.");
                }
            } catch (InputMismatchException e){
                System.out.println("Error! Please enter a digit.");
                counter--; // This is designed to backup the counter so the correct variable can be input into the array
                input.next();
            }
        }
    }
return array;
}

// This will display the current cycle of numbers and format all the data
// and display it appropriatly
public static void displayCurrentCycle() {
    int averageValue = 23; // Filler Variables to make sure code was printing
    int highestValue = 23;
    int lowestValue = 23;
    System.out.println(\n--------------------------------"
            + "\nAverage - " + averageValue 
            + "\nHighest - " + highestValue
            + "\nLowest - " + lowestValue);
}

public static void displayAll() {
    int fullAverageValue = 12; // Filler Variables to make sure code was printing
    int fullHighestValue = 12;
    int fullLowestValue = 12;
    System.out.println(" RESULTS FOR ALL NUMBER CYCLES"
            + "\n--------------------------------"
            + "\nAverage Value - " + fullAverageValue
            + "\nHighest Value - " + fullHighestValue
            + "\nLowest Value - " + fullLowestValue);
}

// This is a basic askToContinue question for the user to decide
public static String askToContinue(Scanner input) {
    boolean loop = true;
    String choice;
    System.out.print("Continue? (y/n): ");
    do {
        choice = input.next();
        if (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n")) {
            System.out.println();
            System.out.println("Final results are listed below.");
            loop = false;
        } else {
            System.out.print("Please type 'Y' or 'N': ");
        }
    } while (loop);
    return choice;
}
}

As far as is understood, your program asks the user to input four digits. This process may repeat and you want to have access to all entered numbers. You're just asking how you may store these.

I would store each set of entered numbers as an array of size four.
Each of those arrays is then added to one list of arrays.

A list of arrays in contrast to a two-dimensional array provides the flexibility to dynamically add new arrays.

We store the digits that the user inputs in array of size 4:

public double[] askForFourDigits() {
    double[] userInput = new double[4];
    for (int i = 0; i < userInput.length; i++) {
        userInput[i] = /* ask the user for a digit*/;
    }
    return userInput;
}

You'll add all each of these arrays to one list of arrays:

public static void main(String[] args) {
    // We will add all user inputs (repesented as array of size 4) to this list.
    List<double[]> allNumbers = new ArrayList<>();

    do {
        double[] numbers = askForFourDigits();
        allNumbers.add(numbers);

        displayCurrentCycle(numbers);
        displayAll(allNumbers);
    } while(/* hey user, do you want to continue */);
}

You can now use the list to compute statistics for numbers entered during all cycles:

public static void displayAll(List<double[]> allNumbers) {
    int maximum = 0;
    for (double[] numbers : allNumbers) {
        for (double number : numbers) {
            maximum = Math.max(maximum, number);
        }
    }
    System.out.println("The greatest ever entered number is " + maximum);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM