随机字符出现在我的输出中,不确定它们来自哪里。

huangapple go评论80阅读模式
英文:

Random characters showing up in my output, not sure where they're coming from

问题

我正在创建一个程序,该程序使用两种方法打印数组中的所有奇数,然后获取奇数的总和。然而,我得到了一个毫无意义的输出。这是我使用的代码:

public class ArrayMethods1 {
    public static int[] printOdds(int[] arrayExample) {
        int i;
        String oddNum = "";
        int[] newArray = new int[3];
        int x = 0;
        for (i = 0; i < arrayExample.length; i++) {
            if (arrayExample[i] % 2 != 0) {
                System.out.print(arrayExample[i] + " ");
            }
        }
        int[] sumOfOdds = new int[1];
        sumOfOdds = sumOdds(newArray);
        return sumOfOdds;
    }
    public static int[] sumOdds(int[] arrayExample1) {
        int i;
        int[] oddsTotal = new int[1];
        int total = 0;
        for (i = 0; i < arrayExample1.length; i++) {
            if (arrayExample1[i] % 2 != 0) {
                total = total + arrayExample1[i];
            }
        }
        oddsTotal[0] = total;
        return oddsTotal;
    }
    public static void main(String[] args) {
        int[] mainArray = new int[5];
        mainArray[0] = 17;
        mainArray[1] = 92;
        mainArray[2] = 21;
        mainArray[3] = 984;
        mainArray[4] = 75;
        printOdds(mainArray);
        int[] oddSum = new int[1];
        oddSum = sumOdds(mainArray);
        System.out.println(oddSum);
    }
}

但我得到的输出是:

17 21 75 [I@51016012

我完全不知道第二部分从哪里来,所以任何帮助都将不胜感激。谢谢!

注意: 这是你提供的代码的翻译,仅包含代码部分,没有附加内容。

英文:

I'm creating a program that uses two methods to print all the odd numbers in an array and then get the sum of the odd numbers. However, I'm getting an output that makes no sense. This is the code that I'm using:

public class ArrayMethods1 {
public static int[] printOdds(int[]arrayExample) {
int i;
String oddNum = &quot;&quot;;
int [] newArray = new int [3];
int x = 0;
for (i = 0; i &lt; arrayExample.length; i++) {
if (arrayExample[i] % 2 != 0) {
System.out.print(arrayExample[i] + &quot; &quot;);
}
}
int[] sumOfOdds = new int [1];
sumOfOdds = sumOdds(newArray);
return sumOfOdds;
}
public static int[] sumOdds(int[]arrayExample1) {
int i;
int[] oddsTotal = new int[1];
int total = 0;
for (i = 0; i &lt; arrayExample1.length; i++) {
if (arrayExample1[i] % 2 != 0) {
total = total + arrayExample1[i];
}
}
oddsTotal[0] = total;
return oddsTotal;
}
public static void main(String[] args) {
int [] mainArray = new int [5];
mainArray[0] = 17;
mainArray[1] = 92;
mainArray[2] = 21;
mainArray[3] = 984;
mainArray[4] = 75;
printOdds(mainArray);
int [] oddSum = new int[1];
oddSum = sumOdds(mainArray);
System.out.println(oddSum);
}
}

And I'm getting this as output:
17 21 75 [I@51016012

I have absolutely no idea where that second part is coming from, so any help would be awesome. Thanks!

答案1

得分: 1

你将总和的结果存储在一个数组中,然后打印 int[] 类型的引用,这就是为什么你得到了 [I@51016012。所以你需要打印 oddSum[0]。

英文:

well you are storing the result of the sum in an array and then you print the reference of type int[], that's why you get [I@51016012. so you need to print oddSum[0].

答案2

得分: 0

以下是翻译好的部分:

这部分代码返回int[]的原因并不太清楚,因为这些方法仅打印和计算奇数的总和。

因此,可以优化代码:

public static void printOdds(int[] arr) {
    for (int n : arr) {
        if (n % 2 == 1) {
            System.out.print(n + " ");
        }
    }
    System.out.println();
}

public static int sumOdds(int[] arr) {
    int total = 0;
    for (int n : arr) {
        if (n % 2 == 1) {
            total += n;
        }
    }
    return total;
}

此外,也可以考虑使用Java 8+的流(stream)来一次性执行这两个任务:

import java.util.Arrays;

public class PrintAndSumOdds {

     public static void main(String [] args){
        int[] arr = {17, 92, 21, 984, 75};
        
        int sumOdds = Arrays.stream(arr) // 从数组获取IntStream
              .filter(n -> n % 2 == 1)   // 过滤出奇数
              .peek(n -> System.out.print(n + " "))  // 在一行中打印它们
              .sum(); // 计算总和(终端操作)
        System.out.println("\nTotal odds: " + sumOdds);
     }
}

输出:

17 21 75 
Total odds: 113
英文:

It is not quite clear why you return int[] from the methods that just print and calculate the sum of the odd numbers.

So the code could be enhanced:

public static void printOdds(int[] arr) {
for (int n : arr) {
if (n % 2 == 1) {
System.out.print(n + &quot; &quot;);
}
}
System.out.println();
}
public static int sumOdds(int[] arr) {
int total = 0;
for (int n : arr) {
if (n % 2 == 1) {
total += n;
}
}
return total;
}

Also, it may be worth to use Java 8+ stream to implement both tasks in one run:

import java.util.Arrays;
public class PrintAndSumOdds {
public static void main(String [] args){
int[] arr = {17, 92, 21, 984, 75};
int sumOdds = Arrays.stream(arr) // get IntStream from the array
.filter(n -&gt; n % 2 == 1)   // filter out the odds
.peek(n -&gt; System.out.print(n + &quot; &quot;))  // print them in one line
.sum(); // and count the sum (terminal operation)
System.out.println(&quot;\nTotal odds: &quot; + sumOdds);
}
}

Output:

17 21 75 
Total odds: 113

huangapple
  • 本文由 发表于 2020年7月23日 03:40:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定