java - 需要从 infile 中取出每个整数并将其传递给方法

标签 java for-loop

我试图从 infile 中读取每个整数,并将其传递给 adScore 方法,该方法确定字母等级、所有等级的总数以及最高考试分数和最低考试分数。但是我在执行 for 循环时的 while 循环并没有从 infile 中提取数据,因为我在 for 循环执行 system.out.print 之后对其进行调试。返回的只是数字 0-29,这是我在循环中的计数器。可以帮助我了解我做错了什么,以便我可以从 infile 中提取成绩分数吗?

问候。

        while(infile.hasNextInt())
        {
            for(int i=0; i <30; i++)//<-- it keeps looping and not pulling the integers from the     file.
            {
                System.out.println(""+i);//<--- I placed this here to test what it     is pulling in and it is just counting
                //the numbers 0-29 and printing them out.  How do I get each data    from the infile to store
                exam.adScore(i);//determines the count of A, B, C, D, F grades, total   count, min and max
            }
        }

最佳答案

它打印 0 - 29,因为这就是你告诉它要做的事情:

System.out.println(""+i)

将打印出 i,它只是您用作循环计数器的整数。您实际上从未从 Scanner 对象中检索下一个值。我猜这是家庭作业,所以我不会给你任何代码,但我会说你肯定需要使用 Scanner 的 nextInt() 方法从输入文件中检索值并在你的文件中使用该值for循环。

关于java - 需要从 infile 中取出每个整数并将其传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158951/

相关文章:

java - 使用integer.parseInt java时出现数字格式异常

java - 如何在java中创建一个每次迭代返回不同变量的循环?

java - 客户端验证最小值为 1 的非强制整数字段

java - TSV 文件转换为 RDF

java - Android Volley 访问 http 响应头域

java - 如何在 Java 的 Calendar 类中获取用户输入的日期而不是当前日期?

c - 为什么我无法初始化指针变量并且无法使用gets?

c - 将 for 循环中的值存储到数组中

javascript - for 循环不进行条件检查

R for循环索引问题