JAVA/JAVA 연습

DO IT 세 값의 최대 값 구하기

HicKee 2022. 10. 31. 22:30

Code

import java.util.Scanner;

public class Max3 {

	public static void main(String[] args) {
		//3개의 정수값을 입력하여 최대값을 구해라
		
		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();
		
		int max = a;
		if (max<b) {
			max = b;
		}
		if(max<c) {
			max = c;
		}
		
		System.out.println("최대값은 "+max);
		
	}

}

Method

import java.util.Scanner;

public class Max3Method {
	
	static int max3(int a,int b,int c) {
		int max = a;
		if (max<b) {
			max =b;
		}
		if(max<c) {
			max = c;
		}
		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.println("최대값은 "+max3(a, b, c));
		
		sc.close();

	}
}

출력

세개의 정수를 입력하세요
a의 값 :1
b의 값 :2
c의 값 :3
최대값은 3