제 작업은 "번호 삽입"과 같이 터미널을 통해 명령을 받고 번호를 사용하여 삽입 방법을 호출하는 것입니다.터미널에서 입력을 받아 올바르게 사용하려면 어떻게해야합니까?
내 방법이 효과적입니다. 이 내 코드입니다 : 내 전체 코드의
String command = Terminal.readLine();
while (command != null) {
switch (command) {
case "insert number":
String[] split = command.split(" ");
linkedTuple.insert(Integer.parseInt(split[1]));
break;
일부만. 제 문제는 "insert number"를 사용하면 터미널에 삽입 번호를 쓰고 대신 3을 삽입하기 위해 예를 들어 3을 쓰고 싶지만 어떻게해야합니까? 케이스? 내 응용 프로그램을 종료하는 quit과 같은 명령이 작동 중이기 때문에 터미널이 작동합니다.
감사합니다.
편집 : 명확하게 내 주요 메도의 경우 :
public static void main(String[] args) {
int[] tuple = { 1 };
LinkedNaturalNumberTuple linkedTuple = new LinkedNaturalNumberTuple(
tuple);
String command = Terminal.readLine();
String[] split = command.split(" ");
while (command != null) {
switch (split[0]) {
case "insert":
linkedTuple.insert(Integer.parseInt(split[1]));
break;
case "remove":
Terminal.printLine(""
+ linkedTuple.remove(Integer.parseInt(split[1])));
break;
case "swap":
if (!linkedTuple.swap(Integer.parseInt(split[1]),
Integer.parseInt(split[2]))) {
Terminal.printLine("Error, your numbers are invalid please try again!");
}
break;
case "min":
if (linkedTuple.min() == -1) {
Terminal.printLine("Error, your tuple is empty, use insert number to insert a number!");
} else {
Terminal.printLine("" + linkedTuple.min());
}
break;
case "max":
if (linkedTuple.max() == -1) {
Terminal.printLine("Error, your tuple is empty, use insert number to insert a number!");
} else {
Terminal.printLine("" + linkedTuple.max());
}
break;
case "info":
Terminal.printLine(linkedTuple.toString());
break;
case "quit":
System.exit(1);
break;
}
command = Terminal.readLine();
}
}
내가 명령을 입력 한 후 첫 번째 대신 호출됩니다 다른 하나를 입력합니다. 예를 들어 : 정보 내 튜플이 이 내 튜플을 종료 인쇄 3 삽입 인쇄 다음 내 튜플 인쇄 등
당신은'스위치 내부에'String'를 썼다을()'그리고 그것은 당신에게 오류를 제공하지 않습니다.? 너는 확인해 줄 수 있니?. – Saif
아니 그게 내게 오류를주지 않을 롤 – NhatNienne
@ Saif 당신은 스위치에서 jdk 7 이후 문자열을 사용할 수 있습니다. – SMA