私は、ユーザーが整数のセットを配列に入力できるプログラムを作成しています。ゼロを入力すると、それらの数値の特性が表示されます。 1つのメソッド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];
}
}
ここでは本当に初歩的なものがあります。私はちょうど再帰の概念を学び始めました。だから、優しくしてください。
それは完全な答えhttp://stackoverflow.com/questions/43148782/how-to-find-a-maximum-number-thats-less-を持っていませんが、この質問は、以前に頼まれました最初の数よりもアレイを使用していますが、それは自宅の仕事の問題のように思え、「質問者」はそれに十分な時間を与えていません。 – iavanish
あなたが問題に直面した場所にコードを置くだけで、他人のために読むのが簡単です。 – Omore
誰かがまったく同じ質問をしましたが、今日私は思います。 – efekctive