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();
}
}
'JAVA > JAVA 연습' 카테고리의 다른 글
DO IT 세 값의 최대 값 구하기 (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 |