저는 초보자 프로그래머로 재귀 사용법을 배우고 있습니다. 내 질문이 매우 기본적인 경우 죄송하지만 내 코드가 추가 값을 출력하는 이유를 알고 싶습니다. 코드를 여러 번 실행했는데 추가 값이 출력됩니다. 코드의 요점은 "Pi"를 가져와 숫자와 "x2"를 가져 오는 것입니다. 그래서, 3.1415 = 6 2 8 2 10. 제가 얻은 결과는 아래와 같습니다. 도와 주셔서 감사합니다!
public class printPi
{
static int x = 1;
static double pi = .314159265359;
public static void main(String[] args)
{
piPrinter(pi);
}
public static void piPrinter(double pi01)
{
if(x!=0)
{
pi = pi*10;
x = (int)(pi%10);
x=x*2;
System.out.println(x);
piPrinter(pi);
}
else
System.out.println("done.");
}
}
내 출력 :
6
2
8
2
10
18
4
12
10
6
10
16
18
18
18
18
12
12
12
0
완료되었습니다. (각 # 사이에 새 줄이 있음)
점의 왼쪽에있는 숫자 만 평가하고 println 후에 "제거"하십시오.
이렇게 :
public class printPi
{
public static void main(String[] args)
{
double pi = 3.14159265359;
piPrinter(pi);
}
public static void piPrinter(double pi01)
{
if ((int) pi01 != 0) {
System.out.println((int) pi01 * 2);
piPrinter((pi01 - (int) pi01) * 10);
} else {
System.out.println("done.");
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다