博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
姓名排序
阅读量:5282 次
发布时间:2019-06-14

本文共 3513 字,大约阅读时间需要 11 分钟。

///yuec2 yue chengpackage lab1;import java.util.Arrays;import java.util.Scanner;/**NameSorter class takes n number of names in the form of string inputs  * from the user. It then asks user for which name to search for.  * It sorts the names entered by the user, and then prints the  * position of the search-name in the sorted list of the names,  * if it is found. */public class NameSorter {    Scanner input = new Scanner(System.in);        /**getNameInputs takes an int parameter n and creates an array of size n.      * It then asks user to Enter n names that get stored in the array.      * It uses the helper method toTitleCase() to convert all names to Title case.      * It returns the array filled with names entered by the user.     */    String[] getNameInputs(int n) {        //write your code here        String[] names = new String[n];        for (int i = 0; i < n; i++) {            names[i] = input.next();        }        return names;    }        /**toTitleCase() takes one string argument as name and returns the string in title case.      * If the name is null or the string is empty, it returns null.     */    String toTitleCase(String name) {        //write your code here        if (name == null || name.length() == 0)             return null;        char c = name.charAt(0);        char cap = Character.toUpperCase(c);        String sub = name.substring(1);        StringBuilder sb = new StringBuilder();        sb.append(cap);        for (int i = 0; i < sub.length(); i++) {            sb.append(Character.toLowerCase(sub.charAt(i)));        }        return sb.toString();    }    /**sortAndSearch() takes two arguments. The first is an array of strings and the second     * is a searchString. The method first sorts the array in increasing alphabetical order,      * and prints it in that order.     * It then searches for the searchString in a case-insensitive way. If the searchString is found,     * it returns the position of the searchString according to the sorted list.      * If it is not found, then it returns -1.     */    int sortAndsearch(String[] strings, String searchString) {        int pos = -1;        if (strings == null || strings.length == 0 || searchString == null)             return pos;        //write your code here        Arrays.sort(strings);        for (int i = 0; i < strings.length; i++) {            if (strings[i].equalsIgnoreCase(searchString))                 return i;        }        return pos;    }    /**DO NOT CHANGE THIS METHOD */    public static void main(String[] args) {        NameSorter ns = new NameSorter();        System.out.println("*** How many names to store? ***");        int n = ns.input.nextInt();        if (n > 0) {            String[] names = ns.getNameInputs(n);            System.out.println("*** Enter the name to search ***");            String name = ns.input.next();            int position = ns.sortAndsearch(names, name);            if (position >=0 ) System.out.println(name + " found at position " + (position+1));            else System.out.println("Sorry! " + name + " not found!");        } else System.out.println("Good Bye!");    }}
View Code

 

读懂题、先写cc  equalsIgnoreCase等给的函数要用

 

Arrays.sort(strings);自动按首字母排序

 

 

注意names[i] = input.next();和nextline()的区别

String[] getNameInputs(int n) {        //write your code here        String[] names = new String[n];        for (int i = 0; i < n; i++) {            names[i] = input.next();        }        return names;    }

 

 

 

 

转载于:https://www.cnblogs.com/immiao0319/p/9644388.html

你可能感兴趣的文章
shell脚本统计文件中单词的个数
查看>>
SPCE061A学习笔记
查看>>
sql 函数
查看>>
hdu 2807 The Shortest Path 矩阵
查看>>
熟悉项目需求,要知道产品增删修改了哪些内容,才会更快更准确的在该项目入手。...
查看>>
JavaScript 变量
查看>>
java实用类
查看>>
smarty模板自定义变量
查看>>
研究称90%的癌症由非健康生活习惯导致
查看>>
命令行启动Win7系统操作部分功能
查看>>
排序sort (一)
查看>>
Parrot虚拟机
查看>>
Teamcenter10 step-by-step installation in Linux env-Oracle Server Patch
查看>>
Struts2学习(三)
查看>>
Callable和Runnable和FutureTask
查看>>
GitHub 多人协作开发 三种方式:
查看>>
文本域添加编辑器
查看>>
Yum安装MySQL以及相关目录路径和修改目录
查看>>
java获取hostIp和hostName
查看>>
关于web服务器和数据库的各种说法(搜集到的)
查看>>