参数、方法和输入

htzpubme  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(334)

我是java的初学者,我只是想知道是否有一种方法可以在多个方法中使用用户的一个输入?我正在制作一个程序,它应该从用户那里获取一些输入(整数)并控制输入,然后计算平均值,最后计算输入的出现次数?我有一个主要方法+3个不同的方法(一个计算平均值等)。我尝试了很多不同的方法,但似乎不理解参数的意义以及它们是如何工作的。
所以这只是一个快速的概述,请记住我是一个初学者!!!!

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("How many elements do you want to enter");

    int value = sc.nextInt(); //Number of how many elements the user want to enter
    int[] input = new int[value]; //An array with all the values

}

public int secureInt(int number, int[] input, int value) { 

    if (!Integer.parseInt(number)) {
        System.out.println("Invalid input");

    } else {
        for (int i = 0; i < value; i++) { //Add all the inputs in the array
            input[i] = sc.nextInt();
        }
    }

    public double averageCalculator (int value, int[] in){
        double average; // The average
        double sum = 0; // The total sum of the inputs

        if (int i = a; i < value; i++) {
            sum = sum + in[i];
        }
        average = sum / value;

        return average;

    }
    //Count the occurence of inputs that only occure once
    public static int countOccurence(//what parameter should i have here?) { 
        int count = 0;

    }
}

提前谢谢!

cidc1ykv

cidc1ykv1#

@艾玛
下面是一些可能对您有帮助的代码。我们的想法是尝试模仿或模仿本摘录中的风格和最佳实践:

import java.util.Scanner;

public class ArrayFiller {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

            System.out.println("How many elements do you want to enter");
            int input_element_count = sc.nextInt(); //Number of how many elements the user want to enter
            int element_count = input_element_count;

        int[] array = new int[element_count]; //An array with all the values
        enter_elements_of_array(array, element_count, sc);
        double average = averageCalculator(array, element_count);
        printArray(array);
        System.out.println("The average of the entered numbers is " + average);

    }

    public static void printArray(int[] array) {

        System.out.print("The array you entered is : [");
        for (int element : array) {
            System.out.print(" " + element + " ");
        }
        System.out.print("]" + "\n");
    }

    public static void enter_elements_of_array( int[] array, int element_count, Scanner sc) { 

            for (int i = 0; i < element_count; i++) { //Add all the inputs in the array
                System.out.println("Please enter element " + (i+1) + " of " + element_count + ":");
                array[i] = sc.nextInt();
            }
    }

        public static double averageCalculator ( int[] array, int element_count){
            double average; // The average
            double sum = 0; // The total sum of the inputs

            for (int i = 0; i < element_count; i++) {
                sum = sum + array[i];
            }
                average = sum / element_count;

            return average;
        }

        //Count the occurence of inputs that only occur once
        public static int countOccurence(int[] array) { 
            int count = 0;
            // algorithm for counting elements with cardinality of 1
            return count;
        }

}

如果你喜欢我的回答,请你投票给我的答案,这样我就可以更充分地参与这些论坛&帮助别人,并得到他们的帮助?我将不胜感激。

相关问题