7-34 通讯录的录入与显示 (10 分)

import java.util.Scanner;
public class Main {
    public static void main (String [] args) {
        Scanner sc = new Scanner(System.in);
        //此处必须是nextLine,否则会出错,因为nextInt读取时忽略掉最后的空格和换行,
        //但是紧接着的nextLine会读取忽略的空格与换行,这样导致str[]中存储的第一个值为空字符串
        int n = Integer.parseInt(sc.nextLine());
        String [] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = sc.nextLine();
        }
        int k = sc.nextInt();
        int index = 0;
        for (int j = 0; j < k; j++) {
            index = sc.nextInt();
            if (index < n && index >= 0) {
                String [] s = str[index].split(" ");
                System.out.println(s[0] + " " + s[3] + " " + s[4] + " " + s[2] + " " + s[1]);
            } else {
                System.out.println("Not Found");
            }
        }
    }
}

参考文章:https://blog.csdn.net/Modelmund/article/details/84632623

上一篇:运算符、包机制与部分Scanner对象方法


下一篇:错误票据