【问题标题】:Why doesn't my code continue?为什么我的代码没有继续?
【发布时间】:2016-02-29 08:38:27
【问题描述】:

当我运行程序时,它会提示我 “请输入包含女孩名字的文件名。” 然后 “请输入包含男孩名字的文件名。” 没有其他反应,但代码没有终止

    public static int getBoys(ArrayList<String> boysNames, Scanner s)         throws IOException
    {
        //Getting boys file location
    System.out.println("Please enter the file name containing the the  boys names.");
    String boyFileLocation=s.nextLine();

    //Opening the file containing the names of boys
    File boyFile = new File(boyFileLocation);
    Scanner BF = new Scanner(boyFile);
    int initialBoyCount=0;

    while(BF.hasNext())
    {
    for (int i=0;i<boysNames.size();i++)
    {
        if(BF.nextLine()==boysNames.get(i))
    {

    }
    else
    {
        boysNames.add(BF.nextLine());
    }
    }
    }
    return(initialBoyCount);
    }

【问题讨论】:

  • 可能你正在无限地添加到boysNames。我认为您删除了 for 循环。
  • 所以你甚至没有添加任何调试来尝试自己解决?
  • 您的代码中没有任何内容显示您打印输入女孩消息或处理女孩输入的位置。
  • 没有冒犯,但是您的代码很糟糕,即使您的代码确实有效,也不显示任何逻辑,您总是会返回 0

标签: java terminate


【解决方案1】:
public static int getBoys(ArrayList<String> boysNames, Scanner s)         throws IOException
{
    //Getting boys file location
System.out.println("Please enter the file name containing the the  boys names.");
String boyFileLocation=s.nextLine();

//Opening the file containing the names of boys
File boyFile = new File(boyFileLocation);
Scanner BF = new Scanner(boyFile);
int initialBoyCount=0;

while(BF.hasNext())
{ BF.next() initialCount++;
}
return(initialBoyCount);
}

如果您只想计算文件中男孩名字的数量。 否则,我不知道你在追求什么

编辑:你有无限循环,因为你没有从扫描仪读取

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 2017-04-18
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多