점프 투 자바(Java)/23년 1월

2월 2일(목) - 9장(종합문제)

C0MPAS 2023. 2. 2. 21:24

9장 - 종합문제

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

4번 - 피보나치 수열

public class Question_4
{
    static int fibonachi(int n) {
        if (n == 0) {
            return 0;
        }
        else if (n == 1)
        {
            return 1;
        }
        else
        {
            return fibonachi(n-2) + fibonachi(n-1);
        }
    }

    public static void main(String[] args)
    {
        for(int i=0; i<10; i++)
        {
            System.out.println(fibonachi(i));
        }
    }
}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

5번 - 한 줄 구구단

import java.util.Scanner;
public class Question_5
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("구구단을 출력할 숫자를 입력하세요(2~9):");
        int num = sc.nextInt();
        for(int i=1; i<10; i++)
        {
            System.out.printf("%d ", i*num);
        }
    }
}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

6번 - 입력 숫자의 총합 구하기

import java.util.Scanner;

public class Question_6
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("입력해주세여:");
        String userInput = sc.nextLine();
        String[] numbers = userInput.split(",");
        int total =0;
        for(String num : numbers)
        {
            num = num.trim();
            int n = Integer.parseInt(num);
            total = total + n;
        }
        System.out.printf("총합은 %d 입니다.\n", total);
    }
}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ