【发布时间】:2015-07-30 11:25:18
【问题描述】:
我有一个来自大学的家庭作业问题,我遇到了麻烦,我想知道是否有人可以就如何处理它给我一些建议。我们使用数组、for 循环、if else 和字符串。
我必须创建一个程序来接收许多人的名字,然后将它们放入一个数组中(我没有问题)然后我需要根据每个名字的第一个字母来分隔数组中的名字: A-G 在一个数组中,字母 H-P 在另一个数组中,其余的在最终数组中。
有人告诉我为此使用字符串函数,但不要使用列表或字符。 这是我到目前为止的代码:
import java.util.Scanner;
public class Party {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // declaring scanner
int numGuests; // declaring the variable for number of guests
System.out.println("please enter the number of guests you are hosting : ");
numGuests = sc.nextInt(); // scanner for user inout num of guests
String[] names = new String[numGuests]; // string for number of guests
System.out.println("Please enter names ");
for(int i = 0; i < names.length; i++) // for loop for inputing the names.
{
names[i] = sc.next();
}
}
}
【问题讨论】:
-
这不是问题,不属于这里。
-
你还没有尝试分开名字。先试试你自己。
-
好吧,它离题了...但仍要向您发送正确的方向...所以对于字符串 charAt(0) 这将给出第一个字符,并在此基础上对它们进行排序.. .
-
看起来不错的代码。你有什么问题?
-
如果你的任务允许的话,你也可以去匹配和一些正则表达式。
标签: java arrays string function