JAVA/JAVA 연습

DO IT 연습 문제 1

HicKee 2022. 10. 31. 22:46

1.네값의 최댓 값을 구하는 메서드 작성

import java.util.Scanner;

public class Max4Method {

	static int max4(int a,int b,int c, int d) {
		int max = a;
		if (max < b) {
			max = b;
		}
		if (max < c) {
			max = c;
		}
		if (max < d) {
			max = d;
		}
		return max;
	}
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("세개의 정수를 입력하세요");
		
		System.out.print("a의 값 :");
		int a = sc.nextInt();
		
		System.out.print("b의 값 :");
		int b = sc.nextInt();
		
		System.out.print("c의 값 :");
		int c = sc.nextInt();
		
		System.out.print("d의 값 :");
		int d = sc.nextInt();
		
		System.out.println("최대 값은 "+max4(a,b,c,d));
		
		sc.close();
	}

}

2.세 값의 최솟값을 구하는 메서드 작성

import java.util.Scanner;

public class Min3Method {
	
	static int Min(int a,int b, int c) {
		int min = a;
		
		if (min>b) {
			min = b;
		}
		if (min>c) {
			min = c;
		}

		return min;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("세개의 정수를 입력하세요");
		
		System.out.print("a의 값 :");
		int a = sc.nextInt();
		
		System.out.print("b의 값 :");
		int b = sc.nextInt();
		
		System.out.print("c의 값 :");
		int c = sc.nextInt();
		
		System.out.println("최소값은 "+Min(a,b,c));
		sc.close();
	}

}

3.네 값의 최솟값을 구하는 메서드 작성

import java.util.Scanner;

public class Min3Method {
	
	static int Min(int a,int b, int c,int d) {
		int min = a;
		
		if (min>b) {
			min = b;
		}
		if (min>c) {
			min = c;
		}
		if (min>d) {
			min = d;
		}

		return min;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("세개의 정수를 입력하세요");
		
		System.out.print("a의 값 :");
		int a = sc.nextInt();
		
		System.out.print("b의 값 :");
		int b = sc.nextInt();
		
		System.out.print("c의 값 :");
		int c = sc.nextInt();
		
		System.out.print("d의 값 :");
		int d = sc.nextInt();
		
		System.out.println("최소값은 "+Min(a,b,c,d));
		sc.close();
	}

}