사용자 정의 함수
package test2;
public class Main {
public static int function(int a, int b, int c) {
int min;
if(a > b)
{
if(b > c)
{
min = c;
}
else
{
min = b;
}
}
else
{
if(a > c)
{
min = c;
}
else
{
min = a;
}
}
for(int i = min; i > 0; i--)
{
if(a % i == 0 && b % i == 0 && c % i == 0)
{
return i;
}
}
return -1;
}
public static int function2(int number, int k) {
for(int i = 1; i <= number; i++)
{
if(number % i == 0)
{
k--;
if(k == 0)
{
return i;
}
}
}
return -1;
}
public static char function3(String input) {
return input.charAt(input.length() - 1);
}
public static int max(int a, int b) {
return (a > b) ? a : b;
}
public static int function4(int a, int b, int c) {
int result = max(a, b);
result = max(result, c);
return result;
}
public static void main(String[] args) {
System.out.println("(400, 300, 750)의 최대 공약수 : " + function(400, 300, 750));
int result = function2(3050, 10);
if(result == -1)
{
System.out.println("3050의 10번째 약수는 없습니다.");
}
else
{
System.out.println("3050의 10번째 약수는 " + result + "입니다.");
}
System.out.println("Hello World의 마지막 단어는 " + function3("Hello World"));
System.out.println("(345, 567, 789) 중에서 가장 큰 값은 " + function4(345, 567, 789));
}
}
반복함수, 재귀함수
package test3;
public class Main {
public static int factorial(int number) {
int sum = 1;
for(int i = 2; i <= number; i++)
{
sum *= i;
}
return sum;
}
public static int factorial2(int number) {
if(number == 1)
return 1;
else
return number * factorial2(number - 1);
}
public static int fibonacci(int number) {
int one = 1;
int two = 1;
int result = -1;
if(number == 1)
{
return one;
}
else if(number == 2)
{
return two;
}
else
{
for(int i = 2; i < number; i++)
{
result = one + two;
one = two;
two = result;
}
}
return result;
}
public static int fibonacci2(int number) {
if(number == 1)
{
return 1;
}
else if(number == 2)
{
return 1;
}
else
{
return fibonacci2(number - 1) + fibonacci(number - 2);
}
}
public static void main(String[] args) {
System.out.println("10 팩토리얼은 " + factorial(10));
System.out.println("10 팩토리얼은 " + factorial2(10));
System.out.println("5번째 피보나치 수는 " + fibonacci(5));
System.out.println("5번째 피보나치 수는 " + fibonacci2(5));
}
}
배열
package test4;
import java.util.Scanner;
public class Main {
public static int max(int a, int b) {
return (a > b) ? a : b;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("생성할 배열의 크기를 입력하세요 : ");
int number = scanner.nextInt();
int[] array = new int[number];
for(int i = 0; i < number; i++)
{
System.out.print("배열에 입력할 정수를 하나씩 입력하세요 : ");
array[i] = scanner.nextInt();
}
int result = -1;
for(int i = 0; i < number; i++)
{
result = max(result, array[i]);
}
scanner.close();
System.out.println(result);
int[] array2 = new int[100];
for(int i = 0; i < 100; i++) {
array2[i] = (int) (Math.random() * 100 + 1);
}
int sum = 0;
for(int i = 0; i < 100; i++)
{
sum += array2[i];
}
System.out.println("100개의 랜덤 정수의 평균 값은 " + sum / 100);
int N = 50;
int[][] array3 = new int[N][N];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
array3[i][j] = (int)(Math.random() * 10);
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
System.out.print(array3[i][j] + " ");
}
System.out.println();
}
}
}
클래스
public class Main {
public static void main(String[] args) {
Node one = new Node(10, 20);
Node two = new Node(30, 40);
Node result = one.getCenter(two);
System.out.println(result.getX()+ " " + result.getY());
}
}
public class Node {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public Node getCenter(Node other) {
return new Node((this.x + other.getX()) / 2, (this.y + other.getY()) / 2);
}
}