传智杯Java终端有一天您厌烦了电脑上又丑又没用的终端,打算自己实现一个 Terminal
作者:互联网
Java思路:使用String的字符串处理以及ArrayList的列表处理。
Java代码:
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
public static ArrayList<String> list = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.nextLine();
for (int i = 0; i < n; i++) {
String str = sc.nextLine();
String[] strArr = str.split(" ");
if ("ls".equals(strArr[0])){
for (int j = 0; j < list.size(); j++) {
System.out.println(list.get(j));
}
}else if ("rm".equals(strArr[0]) && list.indexOf(strArr[1]) != -1){
list.remove(list.indexOf(strArr[1]));
}else if ("touch".equals(strArr[0]) && list.indexOf(strArr[1]) == -1){
list.add(strArr[1]);
}else rename(strArr[1],strArr[2]);
}
}
public static void rename(String fromName,String toName){
if (list.indexOf(fromName) == -1){
list.add(toName);
}else {
list.set(list.indexOf(fromName),toName);
}
}
}
标签:传智杯,Java,String,indexOf,list,strArr,else,终端,public 来源: https://blog.csdn.net/m0_46653805/article/details/122022401