英文:
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("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(Arrays.toString(numbers));
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;
}
}
答案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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论