Java教程

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

本文主要是介绍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

这篇关于7-34 通讯录的录入与显示 (10 分)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!