我有一个大学的家庭作业问题,遇到了麻烦,我想知道是否有人可以给我一些建议。我们正在使用数组,用于循环(如果不是)和字符串。
我必须创建一个程序来接受多个民族的名字,然后将它们放入一个数组(我没有问题),然后我需要根据每个名字的前几个字母将数组中的名字分开:AG放在一个阵列,在另一个阵列中用字母HP表示,其余的则在最后阵列中。
有人告诉我为此使用字符串函数,但不要使用列表或Char。这是我到目前为止的代码:
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();
}
}
}
Please Use below logic and get result according to your requirement..
package com.test;
public class Namessplite {
public static void main(String args[]){
String[] names={"ABC","CAD","JKL","MNO"}; // these are names for example
String[] atog_group = new String[10];
String[] gtopgroup= new String[10];
int j=0,k=0;
for(int i=0;i<names.length;i++){
if((int)names[i].charAt(0)<72 && (int)names[i].charAt(0)>64){
atog_group[j]=names[i];
j++;
}else{
gtopgroup[k]=names[i];
k++;
}
}
System.out.println("A TO G Names Are :: ");
for(int m=0;m<j;m++){
System.out.println(atog_group[m]);
}
System.out.println("G TO P Names Are :: ");
for(int m=0;m<k;m++){
System.out.println(gtopgroup[m]);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句