import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Blog { public static void main(String[] args) { // 2. Scanner 클래스로 -1이 입력될 때까지 // 양의 정수를 입력 받아 저장(List객체)하고 검색하여 // 가장 큰 수를 출력하는 프로그램을 작성하라. List<Integer> list = new LinkedList<>(); Scanner sc = new Scanner(System.in); int i = 0; while (true) { System.out.println("숫자 입력"); try { i = sc.nextInt(); } catch (Exception e) { System.out.println("숫자만 입력"); sc.next(); // 숫자가 아닌 문자를 입력해서 catch 문으로 진입한 경우, // next 함수로 그 문자를 소모 해줘야 다음 nextInt에 영향이 안 간다. continue; } if (i != -1) { if (i <= 0) { System.out.println("양의 정수만 입력"); continue; } list.add(i); } else { break; } } int s = 0; for (int j : list) { if (j > s) { s = j; } } System.out.println(s); } }
댓글
댓글 쓰기