【发布时间】: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方法中。