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
'JAVA > JAVA 연습' 카테고리의 다른 글
DO IT 연습 문제 1 (0) | 2022.10.31 |
---|---|
Do it! 자바 프로그래밍 : 04 제어 흐름 이해 1 (0) | 2022.10.09 |
Do it! 자바 프로그래밍 : 연산자 01 (0) | 2022.10.01 |
JAVA 토끼와 거북이 (최대 공약수 ,최소공배수) (0) | 2022.09.30 |
JAVA 개굴개굴 개구리 (0) | 2022.09.25 |