【发布时间】:2015-08-09 21:30:39
【问题描述】:
在 Java 中,我正在创建一个程序,让用户想到他们认识的人。
然后,我的程序要求他们输入名字的第一个字母和姓氏的最后一个字母,不能有空格。
我希望我的程序然后查看一个全名数组,找到第一个字母与用户输入的第一个字母匹配的那个,以及相应的姓氏的最后一个字母。
到目前为止,这是我的程序:
import java.util.* ;
public class Guesser
{
public static void main(String[] args)
{
Scanner UserInput = new Scanner(System.in);
String [] names = {"firstname lastname " + "etc"}; //example name array
System.out.print( "Hello! I am a robot. I might be smart, but I don't know. Please play a game with me to help me see if I am smart." + "\n" + "What I want you to do is think of someone you know." + "\n" + "Enter the first letter of their first name, and the last letter of their last name. Please no spaces. Then, press enter. " );
String TheirGuess = UserInput.nextLine(); //get their input, assign a string to it
System.out.println("You entered: " + TheirGuess);
char FirstChar = TheirGuess.charAt(0); // get the the first char
char SecondChar = TheirGuess.charAt(1); // get the second char
System.out.println("I will now think of someone whose first name starts with " + FirstChar + " and last name ends with " + SecondChar );
UserInput.close();
}
}
如何在字符串数组中搜索第一个字符为 FirstChar 且最后一个字符为 SecondChar 的名称?
【问题讨论】:
-
我知道这个程序中还没有数组。
-
从用户读取数据作为字符串并使用
charAt或substring。