我正在尝试返回数组的详细信息,但实际上返回的是零。

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

I'm trying to return details of an array, but I am returning zeros instead

问题

以下是您要求翻译的内容:

我只是在寻求帮助,因为我似乎无法弄清楚为什么我的程序打印出零,而不是所请求的内容。

使用Eclipse,我正在读取一个包含整数的文本文件到一个数组中,然后按升序输出其整数,数组中所有整数的乘积,以及数组的最小值和最大值。数组的打印以及排序都正常。我还在本帖的末尾附上了运行程序时输出的照片,以供澄清。非常感谢任何帮助 我正在尝试返回数组的详细信息,但实际上返回的是零。 这也是我在这个论坛上的第一篇帖子,所以我会感谢任何关于如何正确发布的帮助。谢谢,大家。

import java.io.File;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

public class Project1 {
    public static void main(String[] args) throws Exception {
        int i = 0, j;
        int[] numbers = new int[20];
        Scanner inputFile = new Scanner(new File("number.txt"));
        BigInteger product = getMultiplication(numbers);
        int minimum = getMinValue(numbers);
        int maximum = getMaxValue(numbers);

        while (inputFile.hasNext()) {
            numbers[i++] = Integer.parseInt(inputFile.nextLine());
        }

        System.out.println("Numbers in ascending order:");
        Arrays.sort(numbers);
        System.out.println("" + Arrays.toString(numbers));
        System.out.println(" ");

        System.out.println("The product of the numbers in the array is: ");
        System.out.println("" + product);

        System.out.println("The minimum value of the array is: ");
        System.out.println("" + minimum);

        System.out.println("The maximum value of the array is: ");
        System.out.println("" + maximum);

        inputFile.close();
    }

    private static int getMaxValue(int[] numbers) {
        int maximum = numbers[0];
        for (int i : numbers) {
            if (maximum < i)
                maximum = i;
        }
        return maximum;
    }

    private static int getMinValue(int[] numbers) {
        int minimum = numbers[0];
        for (int i : numbers) {
            if (minimum > i)
                minimum = i;
        }
        return minimum;
    }

    private static BigInteger getMultiplication(int[] numbers) {
        BigInteger j = new BigInteger("1");
        for (int i : numbers) {
            j = j.multiply(BigInteger.valueOf(i));
        }
        return j;
    }
}

程序输出见链接:
program output

英文:

I'm just looking for assistance because I can't seem to figure out why my program is printing out zeros rather than what is being requested.

Using Eclipse, I am reading a text file entailing integers into an array and then outputting its integers in ascending order, the product of all integers in the array, and the arrays min and max values. The array prints fine as well as the sorting. I've also attached a photo to the end of this post of the output when I actually run the program for clarification. Any help would be greatly appreciated 我正在尝试返回数组的详细信息,但实际上返回的是零。 Also, this is my first post on this forum so I would appreciate any help to post properly. Thanks, you guys.

import java.io.File;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;//imports scanner class
public class Project1
{
public static void main(String[] args) throws Exception   
{	
int i=0,j;
int[]numbers=new int[20];
Scanner inputFile = new Scanner(new File(&quot;number.txt&quot;));
BigInteger product=getMultiplication(numbers);
int minimum=getMinValue(numbers);
int maximum=getMaxValue(numbers);
while(inputFile.hasNext()) 
{
numbers[i++]=Integer.parseInt(inputFile.nextLine());
}
//System.out.println(Arrays.toString(numbers));
System.out.println(&quot;Numbers in ascending order:&quot;);
Arrays.sort(numbers);
System.out.println(&quot;&quot;+Arrays.toString(numbers));
System.out.println(&quot; &quot;);
System.out.println(&quot;The product of the numbers in the array is: &quot;);
System.out.println(&quot;&quot;+product);
System.out.println(&quot;The minimum value of the array is: &quot;);
System.out.println(&quot;&quot;+minimum);
System.out.println(&quot;The maximum value of the array is: &quot;);
System.out.println(&quot;&quot;+maximum);
inputFile.close();
}
private static int getMaxValue(int[] numbers) {
int maximum=numbers[0];
for(int i:numbers)
{
if (maximum&lt;i)
maximum=i;
}
return maximum;
}
private static int getMinValue(int[] numbers) {
int minimum=numbers[0];
for(int i:numbers)
{
if (minimum&gt;i)
minimum=i;
}
return minimum;
}
private static BigInteger getMultiplication(int[] numbers) {
BigInteger j=new BigInteger(&quot;1&quot;);
for (int i:numbers)
{
j=j.multiply(BigInteger.valueOf(i));
}
return j;
}	
}	

program output

答案1

得分: 1

在输入数字后,将代码更改为以下方式以调用这些方法。

while(inputFile.hasNext()) {
numbers[i++]=Integer.parseInt(inputFile.nextLine());
}

BigInteger product=getMultiplication(numbers);
int minimum=getMinValue(numbers);
int maximum=getMaxValue(numbers);


<details>
<summary>英文:</summary>
You have this:

BigInteger product=getMultiplication(numbers);
int minimum=getMinValue(numbers);
int maximum=getMaxValue(numbers);

while(inputFile.hasNext()) {
numbers[i++]=Integer.parseInt(inputFile.nextLine());
}

Change it to this so you call the methods after you enter the numbers.

while(inputFile.hasNext()) {
numbers[i++]=Integer.parseInt(inputFile.nextLine());
}

BigInteger product=getMultiplication(numbers);
int minimum=getMinValue(numbers);
int maximum=getMaxValue(numbers);

huangapple
  • 本文由 发表于 2020年9月25日 09:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64056411.html
匿名

发表评论

匿名网友

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

确定