Created
April 20, 2020 05:46
-
-
Save dalcon10028/38a6a803a8a2871ea0ac7dfc5bde6d29 to your computer and use it in GitHub Desktop.
정수형 배열의 입력, 삭제 프로그램
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Main { | |
| static LinkedList<Integer> list = new LinkedList<>(); // 정수를 넣을 리스트 | |
| public static void main(String[] args) { | |
| int input; | |
| do{ | |
| input = menu(); | |
| switch (input) { | |
| case 1: showArray(); break; | |
| case 2: addElement(); break; | |
| case 3: deleteElement(); break; | |
| } | |
| }while(input!=4); | |
| } | |
| static int menu(){ | |
| Scanner sc = new Scanner(System.in); | |
| int input; | |
| System.out.println("정수형 배열의 입력, 삭제 프로그램"); | |
| System.out.println("1. 전체배열보기"); | |
| System.out.println("2. 숫자입력"); | |
| System.out.println("3. 숫자삭제"); | |
| System.out.println("4. 종료"); | |
| do{ | |
| System.out.print("번호를 선택하세요 : "); | |
| input = sc.nextInt(); | |
| }while(input<1 || input>4); | |
| return input; | |
| } | |
| // 전체배열보기 | |
| static void showArray(){ | |
| for (int i=0; i<list.size(); i++) | |
| System.out.println("index " + i + " : " + list.get(i)); | |
| } | |
| static void addElement(){ | |
| Scanner sc = new Scanner(System.in); | |
| System.out.print("넣을 숫자를 입력해주세요 : "); | |
| list.add(sc.nextInt()); // 정수 삽입 | |
| Collections.sort(list); // 정렬 | |
| showArray(); // 정렬 후 리스트 보여주기 | |
| } | |
| static void deleteElement(){ | |
| Scanner sc = new Scanner(System.in); | |
| int input; | |
| do{ | |
| System.out.print("삭제할 숫자의 인덱스를 입력해주세요 (0~"+ (list.size()-1) +") : "); | |
| input = sc.nextInt(); | |
| }while(input<0 || input>=list.size()); // 0보다 작거나 최대 인덱스보다 크면 반복 | |
| list.remove(input);// 제거 | |
| showArray(); // 제거 후 리스트 보여주기 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment