我想知道两个不同数组的平均数是多少.实际上是我做的,问题是,我正在进行验证,以确保两个数组具有相同的长度.因此,当我犯了错误(故意)以不同的方式定义其中一个数组的长度时,它会显示错误消息,并返回到定义数组长度,但当我对平均值进行计算时,由于某种原因,它返回错误的结果.从字面上看,一切都运行得很好,除了调用了函数ComparValore()时.

请救救我.

import java.util.Scanner;

public class MediaVetores {
    public static void main(String[] args) {
        int vec1 [];
        int vec2 [];
        vec1 = lerVetor1();
        vec2 = lerVetor2();
        mediaVetores(vec1, vec2);
    }

    static int lerInteiro(String pergunta){
        System.out.println(pergunta);
        Scanner ler = new Scanner(System.in);
        return ler.nextInt();
    }

    static int [] lerVetor1(){
        int tamVetor = lerInteiro("Quantos valores quer inserir no 1º vetor?: ");
        int vec1 [] = new int [tamVetor];
        for (int i = 0; i < tamVetor; i++){
            vec1 [i] = lerInteiro("Insira o valor do " + (i+1) + "º número do vetor: ");
        }
        return vec1;
    }

    static int [] lerVetor2(){
        int tamVetor = lerInteiro("Quantos valores quer inserir no 2º vetor?: ");
        int vec2 [] = new int [tamVetor];
        for (int i = 0; i < tamVetor; i++){
            vec2 [i] = lerInteiro("Insira o valor do " + (i+1) + "º número do vetor: ");
        }
        return vec2;
    }

    static void mediaVetores(int vec1 [],int vec2 [] ){
        int n1 = 0;
        int c1 = 0;
        int n2 = 0;
        int c2 = 0;
        int total = 0;
        int soma = 0;
        double media = 0.0;
        for (int i = 0; i < vec1.length; i++){
            n1 = n1 + vec1[i];
            c1 ++;
        }
        for (int j = 0; j < vec2.length; j++){
            n2 = n2 + vec2[j];
            c2 ++;
        }
        soma = n1 + n2;
        System.out.println(soma);
        total = c1 + c2;
        System.out.println(total);
        compararVetores(vec1, vec2);
        media = (double) soma / (double) total;

        System.out.println("A média dos valores inseridos nos vetores é de " + media);

    }

   static void compararVetores (int a[], int b[]){
        if (a.length != b.length){
            System.out.println("Os vetores devem ter o mesmo número de valores! Volte a introduzir novos tamanhos de vetores.");
            lerVetor1();
            lerVetor2();

        }
    }

}

推荐答案

首先,您必须简化代码:

  • 仅使用Scanner的一个实例
  • 使用一种方法创建两个数组
  • 计算中位数时避免使用未使用的变量

然后你就不需要compararVetores()的方法了.如果必须让两个数组具有相同的长度,则应该要求使用一次数组大小,然后要求分别输入每个数组的元素.

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

    System.out.print("How many values do you want to insert vector?: ");
    int total = scan.nextInt();

    int[] arr1 = createArray(scan, 1, total);
    int[] arr2 = createArray(scan, 2, total);

    mediaVectors(arr1, arr2);
}

private static int[] createArray(Scanner scan, int no, int total) {
    System.out.format("Enter total %d values of vector %d (space or enter are separators): ", total, no);

    int[] arr = new int[total];

    for (int i = 0; i < arr.length; i++)
        arr[i] = scan.nextInt();

    return arr;
}

private static void mediaVectors(int[] arr1, int[] arr2) {
    int total = arr1.length + arr2.length;
    int sum = Arrays.stream(arr1).sum() + Arrays.stream(arr2).sum();
    double media = (double) sum / (double) total;

    System.out.println("Sum of values: " + sum);
    System.out.println("Total values: " + total);
    System.out.format(Locale.ENGLISH, "The average of the values entered in the vectors: %.2f", media);
}

Output:

How many values do you want to insert vector?: 4
Enter total 4 values of vector 1 (space or enter are separators): 1 2 3 4
Enter total 4 values of vector 2 (space or enter are separators): 5 6 7 8
Sum: 36
Total: 8
The average of the values entered in the vectors: 4.50

Java相关问答推荐

在Spring Boot中测试时出现SQL语法错误

当列顺序更改时,Table View列列表的Change. wasPermanted()总是返回假

使用Apache Poi MQLSlideShow,在XSLFTable表中,我们可以在文本段落后面的每个单元格中包含圆角矩形吗?

Java中不同包中的类之间的配置共享

H2弹簧靴试验跌落台

弹簧靴和龙目岛

无法使用Java&;TestContainers获取AWS SQS队列的属性

测试容器无法加载类路径初始化脚本

如何在antlr4中跳过所有反斜杠-换行符而保留换行符?

如何在构建Gradle项目时排除com.google.guava依赖项的一个变体

为什么Collectors.toList()不能保证易变性

在Oracle JDBC连接中,连接失效和身份验证失效是什么意思?

二进制数据的未知编码/序列化

try 使用类来包含JSON响应

Android应用程序为错误的显示类型 Select 尺寸文件

Win32函数的JNA绑定DwmGetColorizationColor返回E_INVALIDARG错误

Java递归泛型是否可以被视为继承和重写的语法糖

为什么Spring要更改Java版本配置以及如何正确设置?

没有Google Play服务,Firebase Auth无法工作

Cucumber中第二个网页的类对象未初始化