次のことが必要なコンプ サイの質問があります。
10 進数xと整数を受け取るメソッドを作成しnます。小数位に丸めます (たとえば、最も近い整数に丸める、最もx近い小数に丸めるなど)。n01
この問題が再帰を使用してどのようにアプローチできるかわかりません。単純すぎるようです。
ここで再帰を使用することは、単に非生産的であるように思われます。
double round(double x, int n) {
    if (n <= 0)
        return Math.round(x);
    return round(x * 10, n - 1) / 10;
}
は有効ですが、不要です。基本的に、そのメソッドは次と同じです。
double round(double x, int n) {
    double factor = Math.pow(10, n);
    return Math.round(x * factor) / factor;
}
このメソッドはおそらくより高速に実行され、 のリスクはありませんStackOverflowError(ただし、 の値が非常に大きい場合に限り、その可能性はほとんどありませんn)。
次のような明確な基本ケースと単純化ケースがある場合には、再帰を使用する必要があります。
n <= 1factorial(n-1)小数点以下n桁に丸めることは、再帰に容易に役立ちません。
ボーナス:任意の基数で最も近いn分の 1 に丸めます。
double round(double x, int n, int radix) {
    double factor = Math.pow(radix, n);
    return Math.round(x * factor) / factor;
}
    これはどう?
double round(double x, int n)
{
    if (n <= 0)
        return Math.round(x);
    return round(x * 10, n - 1) / 10;
}
使用できない場合は、これを少し調整する必要がありますMath.round()。
再帰と解析を使用しましたが、論理的な穴がいくつかあると思います。
public static double problem3(double x, int y)
{
    // Trying to take the double, turn it into a string, manipulate past the decimal
    // probably not the most efficient method..
    String P = Double.toString(x).substring(0, Double.toString(x).length()-1);
    String k =P.substring(P.indexOf('.')+ 1,P.length());
    if (k.length()==y)
    {
        if (P.charAt(P.length()-1)<5)
        {
        return Double.parseDouble(P);
        }
        else
        {double o7 = Double.parseDouble(P)*Math.pow(10, k.length());
        o7++;
        o7=o7/Math.pow(10, k.length());
            return o7;
        }
    }
    else 
    {
        return problem3(Double.parseDouble(P),y);
    }
}