【发布时间】:2017-08-26 06:10:57
【问题描述】:
我正在编写一个程序,允许用户将一组整数输入到数组中,一旦输入零,就会显示这些数字的特征。我对一种方法有疑问:findMaxOfLessThanFirst。当然,它会在数组中找到也小于输入的第一个数字的最大数字。完整代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Assignment9 {
public static void main(String[] args) throws IOException {
int index = 0;
int[] numbers;
numbers = new int[100];
InputStreamReader inRead = new InputStreamReader(System.in);
BufferedReader buffRead = new BufferedReader(inRead);
String line = buffRead.readLine();
try {
while (!line.equals("0") && index < 100) {
numbers[index] = Integer.parseInt(line);
index++;
line = buffRead.readLine();
}
} catch (IOException exception) {
System.out.println("Array index out of bound");
}
int min = findMin(numbers, 0);
int sumAtEven = computeSumAtEvenIndexes(numbers, 0, numbers.length - 1);
int divByThree = countDivisibleBy3(numbers, 0, numbers.length - 1);
int maxLessThanFirst = findMaxOfLessThanFirst(numbers, 1, numbers.length - 1, numbers[0]);
System.out.println("The minimum number is " + min);
System.out.println("The sum of numbers at even indexes is " + sumAtEven);
System.out.println("The count of numbers that are divisible by 3 is " + divByThree);
System.out.println(
"The maximum number among numbers that are less than the first number is " + maxLessThanFirst);
}
public static int findMin(int[] numbers, int index) {
if (index == numbers.length - 1) {
return numbers[index];
} else {
return Math.min(numbers[index], findMin(numbers, index + 1));
}
}
public static int computeSumAtEvenIndexes(int[] numbers, int startIndex, int endIndex) {
if (startIndex == endIndex) {
if (startIndex % 2 == 0) {
return numbers[startIndex];
} else
return 0;
} else {
if (endIndex % 2 == 0) {
return computeSumAtEvenIndexes(numbers, startIndex, endIndex - 1) + numbers[endIndex];
} else {
return computeSumAtEvenIndexes(numbers, startIndex, endIndex - 1);
}
}
}
public static int countDivisibleBy3(int[] numbers, int startIndex, int endIndex) {
if (startIndex == endIndex) {
if (numbers[startIndex] % 3 == 0) {
return +2;
} else {
return 1;
}
} else {
if (numbers[endIndex] == 0) {
return countDivisibleBy3(numbers, startIndex, endIndex - 1);
}
if (numbers[endIndex] % 3 == 0) {
return countDivisibleBy3(numbers, startIndex, endIndex - 1) + 1;
} else {
return countDivisibleBy3(numbers, startIndex, endIndex - 1);
}
}
}
private static int findMaxOfLessThanFirst(int[] numbers, int startIndex, int endIndex, int firstNumber) {
if (startIndex == endIndex) {
if (numbers[endIndex] <= firstNumber)
return numbers[startIndex];
}
int max = findMaxOfLessThanFirst(numbers, startIndex, endIndex - 1, firstNumber);
if (max >= numbers[endIndex] && max <= firstNumber) {
return max;
}
return numbers[endIndex];
}
}
我确定我在这里遗漏了一些非常基本的东西。我刚开始学习递归的概念。所以,请温柔一点。
【问题讨论】:
-
这个问题之前有人问过,虽然它没有完整的答案stackoverflow.com/questions/43148782/… 但它似乎是一个家庭作业问题,“提问者”没有给它足够的时间。
-
只将代码放在您遇到问题的地方,以便其他人易于阅读。
-
我想今天有人问了完全相同的问题。
-
在决定发帖之前,我尝试了很多思考。我的错。不知道还有其他人问同样的问题。
标签: java arrays recursion stack-overflow