英文:
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 = "";
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);
}
}
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 + " ");
}
}
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 -> n % 2 == 1) // filter out the odds
.peek(n -> System.out.print(n + " ")) // print them in one line
.sum(); // and count the sum (terminal operation)
System.out.println("\nTotal odds: " + sumOdds);
}
}
Output:
17 21 75
Total odds: 113
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论