- package cn.itcast_03;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- //编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
- public class test03 {
- public static void main(String[] args) {
- List<String> c = new ArrayList<String>();
- Scanner sc = new Scanner(System.in);
- while (true) {
- String s = sc.nextLine();
- if ("end".equals(s)) {
- break;
- }
- c.add(s);
- }
- System.out.println(c);
- for (int i = 0; i < c.size()-1; i++) {
- for (int j = 0; j < c.size()-i-1; j++) {
- if ((((String)c.get(j)).compareTo(((String)c.get(j+1))))>0) {
- String s = (String)c.get(j);
- c.set(j, c.get(j+1));
- c.set(j+1, s);
-
- }
- }
- }
- System.out.println(c);
- }
- }
复制代码 |
|