【问题标题】:Invoking to type the team name twice instead of once [duplicate]调用两次而不是一次键入团队名称[重复]
【发布时间】:2014-04-02 22:17:10
【问题描述】:

我被要求检查团队名称是否存在于我在计算机上创建的文本文件中。我已经编写了完整的代码,但输出总是要求我在计算团队名称出现在文件中的次数之前输入两次团队名称。请务必看到这个,并让我知道。谢谢。

import java.util.*;
import java.io.*;
public class worldSeries 
{
    public String getName(String teamName)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the Team Name : " );
        teamName = keyboard.nextLine();
        return teamName;
    }

    public int checkSeries1 () throws IOException
    {
        String teamName="";


        String[] winners = new String[50];
        int  i = 0 ;
        File file = new File ("WorldSeriesWinners.txt");
        Scanner inputFile = new Scanner(file);
        while ( inputFile.hasNext () && i < winners.length )
        {
            winners[i] = inputFile.nextLine(); 
            i++;
        }
        inputFile.close();

        int count = 0;
        String nameOfTeam = getName(teamName);

        for ( int  index = 0 ; index < winners.length ; index ++ )
        {
            if ( nameOfTeam.equals(winners[index]))
            {
                count++;

            }
        }
        return count;

    }

    public static void main(String[]Args)
    {
        String teamName = "";
        worldSeries object1 = new worldSeries();

        try
        {
            System.out.println(" The Number of times " + object1.getName(teamName) + "won the Championship is : " +object1.checkSeries1());
        }
        catch ( IOException ioe )

        {
            System.out.println(" Exception!!! ");

            ioe.printStackTrace();
        }

    }
}

【问题讨论】:

  • 你为什么在similar question下发布different name??
  • 只要在getName()方法中放一个断点,看看什么时候被调用,然后使用调用栈(或者直接跳出来)看看是从哪里调用的。

标签: java methods printing


【解决方案1】:

数一数你给getTeamName()打了多少次电话——你打了两次。所以你会看到两次。

更重要的是,WorldSeries 类中可能不应该有任何 Scanner 对象或 IO。相反,它应该保存 WorldSeries 信息并具有根据数据检查团队名称的方法。所有用户 I/O 都应该在你的 main 方法中完成(至少在这个程序中)。

【讨论】:

  • 我不会这样做两次。当我将它分配给 String nameOfTeam 时,我只看到它一次。
  • @user3369285:不要相信我的话,使用你的IDE来计算你调用它的次数。严重地。该方法也不应该带参数。
  • 我的教授要我在课堂上做,而不是在 main 方法中
  • 再次数一数。文字处理器或 IDE 不会说谎。只需在上面的文件中搜索 getName。
  • 我只是用我的 word find 选项做了三次。一个当我声明 getName() 类,然后当我分配 newName,然后当我在 main 方法中打印它时
【解决方案2】:

您的代码正在这样做:

System.out.println(" The Number of times " + object1.getName(teamName) + "won the Championship is : " +object1.checkSeries1());

您的 getName 方法正在提示输入名称 - 它在上面的行中被直接调用,也被间接调用(在同一行的 checkSeries1 内)。所以这意味着它在该行中被调用了两次......

您将需要重新考虑提示在哪里完成并进行一些重构以修复它。

【讨论】:

  • 你能告诉我我可以做些什么来让它变得更好吗?
  • 一个起点可以是将您的提示放入一个名为promptForName 的函数中,并使getName 更简单?然后,您可以随意拨打getName,而无需再次提示。
  • 1+ 的好建议。 @user3369285:你真的需要在这里自己做一些思考。否则你永远学不会编程。
【解决方案3】:

这是在 Java 中执行此操作的一种方法:

import java.io.*;

public class WorldSeries
{
    /**
     * Count the number of lines a string occurs on in a file.
     */
    public static final void main(String[] argv)
        throws IOException
    {
        if(argv.length<2)
        {
            showUsage();
            System.exit(-1);
        }

        int count=0;
        String term = argv[0];
        String filename = argv[1];

        LineNumberReader reader = new LineNumberReader(
            new FileReader(filename)
            );

        for(String line=reader.readLine(); line != null; line=reader.readLine())
        {
            if(line.indexOf(term) > -1)
            {
                count++;
            }
        }

        System.out.println(count);
    }

    private static final void showUsage()
    {
        System.out.println("Search for term in a file.");
        System.out.println("USAGE: <term> <file-name>");
    }
}

但您也可以通过脚本来做到这一点:

grep -c "Boston Red Sox" world-series.txt

【讨论】:

  • 如果您要对一个在阅读该问题时正确的答案投反对票,最好为您的反对票提供解释。
猜你喜欢
  • 2019-11-07
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 2022-11-29
  • 1970-01-01
相关资源
最近更新 更多