【问题标题】:How do I call from a list of names?如何从姓名列表中调用?
【发布时间】:2014-12-27 00:20:44
【问题描述】:

我才学了几个星期的 java,所以我还是个菜鸟。我希望下一行打印“先生/小姐”。 + 名字 + 姓氏; 但是,我不想输入此人的性别。我希望有一个男性姓名列表(超过 1000 个姓名的长列表)以及用于检测名字输入是否是其中一个男性姓名的程序。然后程序可以分配正确的标题 (mr/ms.)

如何在不创建普通数组列表并单独输入每个名称的情况下执行此操作(这将花费很长时间)。

提前致谢!

public static void main (String [] args){

    Scanner scanner = new Scanner(System.in);
    System.out.println("Hello, will you be checking out today? (Y/N)");
    String CheckOutYesOrNo = scanner.nextLine();

    if (CheckOutYesOrNo.equalsIgnoreCase("y")) { x();}
    else if (CheckOutYesOrNo.equalsIgnoreCase("n")) {System.out.println("Okay then. Enjoy the rest of your stay at the Rizty Hotel!");}
    }

    public static void x(){
        System.out.println("Sure, would you mind telling me your last name?");
        Scanner scanner = new Scanner(System.in); //how can I avoid making a new scanner?
        String lastname = scanner.nextLine();

        System.out.println("And your first name?");
        String firstname= scanner.nextLine();





    }
}

【问题讨论】:

  • 那两个名字呢? (贝利、山姆等)
  • 你有 .txt 文件中的名字吗?
  • 基本上每个系统都会从用户那里收集头衔,而不仅仅是性别,这是有原因的。
  • 你有没有想过unisex name?在这种情况下你会显示什么?
  • “我怎样才能避免制作新的扫描仪?”将scanner 设为主类中的私有成员,而不是将其设为局部变量。然后您需要使用非静态方法访问它,因此您的main 将类似于new MainClass().doMain(),并且您的代码将在非静态doMain 方法中。

标签: java methods arraylist


【解决方案1】:

听起来像是典型的映射。您可以将所有姓名读入Map,其中键是姓名,性别是值。要解决性别问题,只需执行 genderMap.get(name) 之类的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2014-10-07
    • 2013-09-22
    • 1970-01-01
    • 2017-01-07
    • 2021-07-15
    相关资源
    最近更新 更多