[LOL 자유랭크 분석] #2. 명령어 입력 받기

[LOL 자유랭크 분석] #2. 명령어 입력 받기

#1 https://fatshark.tistory.com/4 에서 만든 Members.txt에 새 멤버를 추가하거나 기존 멤버를 삭제하는 명령어를 만들어서 관리하려고 한다.

검색한 내용

1. Scanner로 입력받는 법

2. Switch-case문+break 사용하는 법

3. continue 사용하기 위해 while(1) 넣기

4. 프로그램 종료하는 법 System.exit(0);

아직 모르는 점

1. #1에서 이어서 Scanner sc를 왜 만들어놔야 하는지

해결해야 되는 문제

1. Add나 Delete 명령어를 입력했을 때 Scanner로 입력값을 받기 전에 프로그램이 종료되어 버린다.

import java.util.Scanner; public class Command { public static void main(String[] args) { String command; //명령어 입력받음 Scanner sc = new Scanner(System.in); System.out.println("명령어를 입력하세요.(Add, Delete, Close)"); command = sc.next(); while(true) { switch(command) { //새 멤버 추가 case "Add": case "add": System.out.println("추가할 멤버의 소환사 이름을 입력하세요."); String addName = sc.nextLine(); break; //기존 멤버 삭제 case "Delete": case "delete": System.out.println("제외할 멤버의 소환사 이름을 입력하세요."); String deleteName = sc.nextLine(); break; //파일 종료 case "Close": case "close": break; default: System.out.println("존재하지 않는 명령어입니다."); System.out.println("명령어를 입력하세요.(Add, Delete, Close)"); command = sc.next(); continue; } break; } } }

from http://fatshark.tistory.com/5 by ccl(A) rewrite - 2021-12-22 01:28:13