// Рекурсивный метод, который принимает ваш int, печатает это количество звезд. // Я знаю, что первый оператор if не является надлежащим java-форматом, но имея nStars (n) в той же строке, что и factorial = (n-1), дает мне ошибку при компиляции.Проблемы с рекурсии. Почему этот метод компилируется, но сбой, когда я запускаю его в своем драйвере?
public class RecursiveMethods {
int theVal;
int factorial;
public void nStars(int n) {
if (n > 0) {
factorial = (n-1);
nStars(n);
System.out.print("*");
}
else {
System.out.print("*");
}
}
// recursively finds the binary of the int and prints the amount of 1s in the binary
public int numOnes(int x) {
int theVal = 0;
if (x == 0) {
theVal = theVal;
}
if (x % 2 == 1) {
theVal = 1 + theVal;
x = (x/2);
numOnes(x);
}
else if (x % 2 == 0) {
theVal = 0 + theVal;
x = (x/2);
numOnes(x);
}
return theVal;
}
}
// here is the driver (Only done for the * method haven't gotten to numOnes driver)
import java.util.*;
public class RecursiveDriver {
private static Object userInput;
public static void main(String[] args) {
RecursiveDriver rd = new RecursiveDriver();
RecursiveMethods rm = new RecursiveMethods();
System.out.println("How many stars do you want to see?");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
rm.nStars(n);
}
}
Какая ошибка? – CoverosGene