[英]How can I receive an input from my Terminal and use it properly
My task is to receive a command via the Terminal like "insert number" and call an insert methode using the number. 我的任务是通过终端接收“插入号码”之类的命令,并使用该号码调用insert方法。
My methode is working. 我的方法正在工作。 This is my code snippet: 这是我的代码段:
String command = Terminal.readLine();
while (command != null) {
switch (command) {
case "insert number":
String[] split = command.split(" ");
linkedTuple.insert(Integer.parseInt(split[1]));
break;
only a part of my complete code. 仅是我完整代码的一部分。 My problem is if I use case "insert number" it will only work if I really would write insert number in my terminal but instead I want to write for example insert 3 to insert the number 3 but how can I call that in my switch case? 我的问题是,如果我使用大小写“ insert number”,它只有在我真的会在终端中写插入号的情况下才起作用,但是我想写例如insert 3来插入数字3,但是如何在我的开关盒中调用它? My Terminal is working because a command like quit which is quitting my Application is working. 我的终端机正在工作,因为诸如quit之类的命令正在退出我的应用程序。
Thank you! 谢谢!
Edit: For clarity my main methode: 编辑:为清楚起见,我的主要方法是:
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();
}
}
If I enter a command and then want to enter another one the first one will be called instead. 如果我输入了一个命令,然后又想输入另一个命令,则第一个命令将被调用。 for example: info then my tuple is printed insert 3 my tuple is printed quit my tuple is printed etc 例如:信息,然后打印我的元组插入3我的元组被打印退出我的元组被打印等等
Use something like: 使用类似:
String[] array = "insert number".split(" ");
....
switch (array[0]) {
case "insert":
In addition to above you need to swap the order of statement from 除上述内容外,您还需要交换以下语句的顺序:
String command = Terminal.readLine();
String[] split = command.split(" ");
while (command != null) {
To 至
String command = Terminal.readLine();
String[] split;
while (command != null) {
command = Terminal.readLine();
split = command.split(" ");
So that you could loop in and ask for user input every time and take split from new command that user has entered. 这样您就可以循环进入并每次都要求用户输入,并从用户输入的新命令中分离出来。
Answer after your edit: 编辑后回答:
At the end of your loop you do 在循环结束时,
command = Terminal.readLine();
But you don't split the new command. 但是,您无需拆分新命令。 As a result, your switch statement still looks at the old split. 结果,您的switch语句仍然查看旧的拆分。
So you need to change the end of the loop to: 因此,您需要将循环的结尾更改为:
command = Terminal.readLine();
split = command.split(" ");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.