我正在编写一个程序,将txt文件中的值导入到数组中,然后我需要计算这些元素中有多少大于或等于36.数据导入正常,以及值的总量它显示是正确的,但我无法显示它在文件中找到数字36的次数。谢谢你的帮助!
public static void main(String[] args) throws Exception {
int[] enrollments = new int [100];
int count;
int FullClass;
double ClassPercentage;
return count (number of data items)
count = CreateArray(enrollments);
System.out.println (count );
FullClass = AddValues (enrollments);
System.out.println (FullClass)
ClassPercentage= FullClass/count;
System.out.print(ClassPercentage +"% of classes are full");
}//end main
/**
*
* @param classSizes
*/
public static int CreateArray(int[] classSizes) throws Exception{
int count = 0;
File enrollments = new File("enrollments.txt");
Scanner infile = new Scanner (enrollments);
while (infile.hasNextInt()){
classSizes[count] = infile.nextInt();
count++}//end while
return count; //number of items in an array
} // end CreateArray
/**************************************************************************/
/**
*
* @throws java.lang.Exception
*/
public static int AddValues (int[] enrollments) throws Exception{
{
int number = 0;
int countOf36s = 0;
while (infile.hasNextInt()) {
number = infile.next();
classSizes[count] = number;
if(number>=36) {
countOf36s++;
}
count++;
}
return countOf36s;
}// end AddValues
}//end main
答案 0 :(得分:0)
在您仅读取文件时,请尝试使用此代码计算大于或等于36的数字。更改createArray
方法中的代码,或者根据需要编写以下逻辑。
我尝试执行此程序。它按预期工作。见下面的代码
import java.util.*;
import java.io.*;
public class Test { //Name this to your actual class name
public static void main(String[] args) throws Exception {
int[] enrollments = new int [100]; //assuming not more than 100 numbers in the text file
int count; //count of all the numbers in text file
int FullClass; //count of numbers whose value is >=36
double ClassPercentage;
count = CreateArray(enrollments);
System.out.println (count);
FullClass = AddValues (enrollments);
System.out.println (FullClass);
ClassPercentage= FullClass/count;
System.out.print(ClassPercentage +"% of classes are full");
}
//Method to read all the numbers from the text file and store them in the array
public static int CreateArray(int[] classSizes) throws Exception {
int count = 0;
File enrollments = new File("enrollments.txt"); //path should be correct or else you get an exception.
Scanner infile = new Scanner (enrollments);
while (infile.hasNextInt()) {
classSizes[count] = infile.nextInt();
count++;
}
return count; //number of items in an array
}
//Method to read numbers from the array and store the count of numbers >=36
public static int AddValues (int[] enrollments) throws Exception{
int number = 0;
int countOf36s = 0;
for(int i=0; i<enrollments.length; i++) {
number = enrollments[i];
if(number>=36) {
countOf36s++;
}
}
return countOf36s;
}
}
答案 1 :(得分:0)
您的代码表明您可能误解了一些概念和风格。正如你在评论中所说,你是新手,想要一些指导以及问题的答案 - 这就是:
方法名称和变量名称按惯例编写,以小写字母开头,然后以驼峰形式开头。这与以大写字母和驼峰大小写开头的类形成对比。坚持这些约定使代码更易于阅读和维护。 full list of conventions is published - 此评论特别指naming conventions。
同样,by convention,关闭大括号会在关闭循环或if-else
阻止时放在单独的行上。
throws Exception
非常通用 - 通常会尽可能地限制您的代码实际抛出的异常 - 在您的情况下throws FileNotFoundException
应该这就是Scanner
或File
可以在运行时抛出的内容。这种特异性对于将来使用任何代码的任何代码都很有用。
您正在创建具有100个成员的阵列。然后调用CreateArray
从文件中读取文件,同时该文件中包含更多整数。您的代码不知道有多少 - 我们称之为N
。如果N <= 100
(有100个或更少的整数),那很好,您的数组将从0
填充到N-1
。但是,这种方法容易引起混淆 - 无论从文件中读取多少值,数组的长度都将为100,因此您必须跟踪count
返回的CreateArray
。
如果N > 100
您遇到问题 - 文件读取代码将继续运行,尝试将数字添加到超出其最大索引的数组中,您将收到运行时错误(索引超出范围)
更好的方法可能是让CreateArray
返回ArrayList
,其中可以有动态长度,您可以使用ArrayList.size()
您的original version AddValues
第二次调用CreateArray
,即使您传入已包含从文件中读取的值的数组。这是低效的,因为它再次执行所有文件I / O.这个小例子不是问题,但你应该避免重复。
主要问题。 As per prudhvi您正在检查文件中的整数对36,而不是每个值。你可以按照答案中的建议纠正这个问题。
你做ClassPercentage= FullClass/count;
虽然ClassPercentage
是双重的,但在某种程度上反直觉 - because both the variables on the Right Hand Side (RHS) are int
, you will have an int
returned from the division总是向下舍入为零。为了使其正常工作 - 您必须更改(cast)RHS上的一个变量,以便在分割前加倍,例如ClassPercentage= ((double)FullClass)/count;
。
如果您继续使用数组而不是ArrayList
,请注意将它们传递给方法时会发生什么。您正在通过引用传递,这意味着如果您在方法中更改数组的元素,则从该方法返回时它将保持更改。
在您的new version中
...
classSizes[count] = number;
if(number>=36) {
...
你几乎肯定是指
...
number = classSizes[count];
if(number>=36) {
...
也就是说,在编写赋值顺序等于是非常重要的,因此a = b
不等同于 到b = a
清理版代码 - 观察以上所有内容(我希望):
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ClassCounter
{
public static void main(String[] args) throws FileNotFoundException
{
int count;
int fullClass;
double classPercentage;
ArrayList<Integer> enrollments = createArray();
count = enrollments.size();
System.out.println(count);
fullClass = addValues(enrollments);
System.out.println(fullClass);
classPercentage = fullClass / count;
System.out.print(classPercentage + "% of classes are full");
}
/**
* scans file "enrollments.txt", which must contain a list of integers, and
* returns an ArrayList populated with those integers.
*
* @throws FileNotFoundException
*/
public static ArrayList<Integer> createArray() throws FileNotFoundException
{
ArrayList<Integer> listToReturn = new ArrayList<Integer>();
File enrollments = new File("enrollments.txt");
Scanner infile = new Scanner(enrollments);
while (infile.hasNextInt())
{
listToReturn.add(infile.nextInt());
}
return listToReturn;
}
/**
* returns the number of cases where enrollments >= 36 from the list of
* all enrollments
*
* @param enrollments - the list of enrollments in each class
* @throws FileNotFoundException
*/
public static int addValues(ArrayList<Integer> enrollments)
{
int number = 0;
int countOf36s = 0;
int i = 0;
while (i < enrollments.size())
{
number = enrollments.get(i);
if (number >= 36)
{
countOf36s++;
}
}
return countOf36s;
}
}