【发布时间】:2016-12-02 22:00:09
【问题描述】:
我正在尝试编写一个代码,该代码将使用 main 来询问用户想要的数字数量,然后用户输入它们。然后在外部方法中,我需要从数组中获取这些数字并找到既是偶数又大于 60 的数字。之后,我需要将这两个数字都打印回主数组。这是我所拥有的:
public class arrayCounter
{//begin class
public static void main(String[]args)
{//begin main method
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println();
double greaterThan = 0;
double evenNumber = 0;
System.out.println("Please enter the number of intergers you want to put into the array.");
int numbers = input.nextInt();
int[] arrayInt = new int[numbers];
int i = 0;
for (i =0; i < arrayInt.length; i++){//begin for loop
System.out.println("Please enter in those numbers now.");
arrayInt[i] = input.nextInt();
}//end for loop
System.out.println("There are " + greaterThan + " numbers that are greater than 60.");
System.out.println("The even numbers are " + evenNumber + " in the array.");
}//end main
public static double arrayCounter (double greaterThan, double evenNumber){//begins outside method
int i = 0;
int numbers = 0;
int[] arrayInt = new int[numbers];
for (i = 0; i < arrayInt.length; i++){//begins for loop
if(arrayInt[i] <= 60){//begin if loop
greaterThan++;
return greaterThan;
}//end if loop
}//end for loop
if(arrayInt[i] % 2 == 0){//begin if loop
evenNumber++;
return evenNumber;
}//end if loop
return 0;
}//end outside method
}//end class
当我在 NetBeans 和 Putty 中编译它时,它编译得很好,但它实际上并没有在答案中给我任何数字,只有 0。它会说: “有 0.0 个数字大于 60。 数组中的偶数为 0.0。”
我做错了什么没有得到正确的数字?
【问题讨论】:
-
这里有一些问题。