此代码的过程必须循环直到 1000 名申请人输入他们的姓名和性别,但在第一个申请人处停止。请帮我看看是怎么回事。我尝试了不同的方法,但没有奏效。
这是代码:
import java.util.Scanner;
public class Problem5 {
public static void main (String [] args)
{
Scanner myVar = new Scanner (System.in);
int b = 1000;
String [] name = new String [b];
/*enter name and gender of applicants*/
for (int i=0; i<name.length; i++)
{
int index = i;
System.out.println("Enter name of applicant and gender:");
System.out.println("M for male and F for female");
name[index] = myVar.nextLine();
for (String name1:name)
{
int maleCount = 0;
int femaleCount = 0;
if (name1.contains("M"))
{
maleCount++;
}
if (name1.contains("F"))
{
femaleCount++;
}
System.out.println("NUMBER OF MALE: "+maleCount);
System.out.println("NUMBER OF FEMALE: "+femaleCount);
}
}
}
}
结果必须如下所示。例如,我输入我的名字 Isabella Cruz 和我的性别 F。
Enter name of applicant and gender:
M for male and F for female
Isabella Cruz F
NUMBER OF MALE: 0
NUMBER OF FEMALE: 1
但是,它停在那里并出现错误: 在 Problem5.main(Problem5.java:30) 线程“main”java.lang.NullPointerException 中出现异常
我需要解决方案,请。
胡说叔叔
潇潇雨雨
相关分类