💻 하나씩 차곡차곡/자료구조 & 알고리즘(JAVA)
세 값의 최댓값 구하기 (chap01/Max3)
뚜루리
2022. 10. 4. 13:08
728x90
320x100
package chap01;
import java.util.Scanner;
public class Max3_221004 {
// 세개의 정수값을 입력하고 최댓값을 구하여 출력.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("세 정수의 최댓값을 구합니다.");
System.out.print("a의 값 : "); int a = scan.nextInt();
System.out.print("b의 값 : "); int b = scan.nextInt();
System.out.print("c의 값 : "); int c = scan.nextInt();
int max = a;
if (b > max) max = b;
if (c > max) max = c;
System.out.println("최대값은 " + max + " 입니다.");
}
}
세 값을 무작정 비교하려 하지 말고, 한 값을 max값으로 정해놓은 다음에 각각의 if문을 이용해 max값과 비교하면 간편하게 비교할 수 있음.
728x90
320x100