// int を受け取り、その数の星を出力する再帰メソッド。//最初の if ステートメントが適切な Java 形式ではないことはわかっていますが、階乗 = (n-1) と同じ行に nStars(n) があると、コンパイル中にエラーが発生します。
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);
}
}