我的 Java 程序中有关于输入姓名和性别的错误。计算并打印男性和女性的数量并重复该过程

此代码的过程必须循环直到 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 中出现异常


我需要解决方案,请。


摇曳的蔷薇
浏览 156回答 2
2回答

胡说叔叔

name具有"Isabella Cruz F"在索引0和阵列的其余部分是nullS,SO name1是null在第二次迭代。从第一个循环中取出第二个循环。for (int i = 0; i < name.length; i++) {&nbsp; &nbsp; //...}for (String name1 : name) {&nbsp; &nbsp;//...}您也可以只使用一个循环并在那里获取数据int maleCount = 0;int femaleCount = 0;for (int i = 0; i < name.length; i++){&nbsp; &nbsp; int index = i;&nbsp; &nbsp; System.out.println("Enter name of applicant and gender:");&nbsp; &nbsp; System.out.println("M for male and F for female");&nbsp; &nbsp; name[index] = myVar.nextLine();&nbsp; &nbsp; if (name[index].contains("M")) {&nbsp; &nbsp; &nbsp; &nbsp; maleCount++;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; else if (name[index].contains("F")) {&nbsp; &nbsp; &nbsp; &nbsp; femaleCount++;&nbsp; &nbsp; }}System.out.println("NUMBER OF MALE: " + maleCount);System.out.println("NUMBER OF FEMALE: " + femaleCount);

潇潇雨雨

有几个问题。首先是有一个嵌套的 for 循环。我看到您首先填充 String 数组,然后对其进行迭代以检查计数。你真的只需要一个循环。其次是您正在检查字符串是否包含“M”或“F”,如果人名包含该字符串(Ex Joanna M Smith F),这可能会出现问题。您只需要检查最后一个字符。import java.util.Scanner;public class Problem5 {&nbsp; &nbsp; public static void main (String [] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Scanner myVar = new Scanner (System.in);&nbsp; &nbsp; &nbsp; &nbsp; int b = 1000;&nbsp; &nbsp; &nbsp; &nbsp; String [] names = new String [b];&nbsp; &nbsp; &nbsp; &nbsp; int maleCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; int femaleCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; /*enter name and gender of applicants*/&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<name.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter name of applicant and gender:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("M for male and F for female");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = myVar.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names[i] = line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check if the line passed in is a M or F&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.substring(line.length()-1).equals("M"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maleCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.substring(line.length()-1).equals("F"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; femaleCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("NUMBER OF MALE: "+maleCount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("NUMBER OF FEMALE: "+femaleCount);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java