有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在数组中多次存储用户输入

我在做一个项目

允许用户输入4个数字,然后存储在数组中供以后使用。我还希望每次用户决定继续该程序时,它都会创建一个新的数组,可以与以后的数组进行比较,以获得最高的平均值、最高值和最低值

代码还没有完成,我知道有些事情仍然需要一些工作。我只是提供了整个代码供参考

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

*我相信我应该使用二维阵列,但我不知道从哪里开始。如果我需要解释更多,请让我知道。(为了以防万一,我在代码中加入了尽可能多的注释。)

我试着转换inputDigit();方法接受二维数组,但无法计算它

如果这个问题之前已经得到了回答,请将我重定向到相应的链接

谢谢!

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;
}
}

共 (1) 个答案

  1. # 1 楼答案

    据了解,您的程序要求用户输入四位数字。此过程可能会重复,您希望能够访问所有输入的号码。你只是在问如何储存这些

    我会将每一组输入的数字存储为一个大小为4的数组
    然后将这些数组中的每一个添加到一个数组列表中

    与二维数组相比,数组列表提供了动态添加新数组的灵活性

    我们将用户输入的数字存储在大小为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;
    }
    

    您将把所有这些数组添加到一个数组列表中:

    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 */);
    }
    

    现在,您可以使用该列表计算所有周期中输入的数字的统计信息:

    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);
    }