java从控制台读取数据的方式

时间:2022-07-22 17:38:33

1、

Scanner sc = new Scanner(System.in);

String s = sc.readLine();

2、

BufferReader br = new BufferReader(new InputStreamReader(System.in));

br,readLine();

实例:

从控制台读出一组数据,并按指定排序:

Scanner sc = new Scanner(System.in);

String s = "";

List<String> ls = new ArrayList<String>();

while("end".equals(s=sc,readLine())) {

ls,add(s);

}

String sort = ls.get(1);

String[] array= ls.get(0).split(",");

List<String> list = Arrays.asList(array);

if ("up".equals(sort)) {

Collections.sort(list)

} else if ("down".equals(sort)) {

Collections.sort(list);

Collections.reverse(list)

}

print(new Gson().toJson(list));