"找不到符号"构造函数的问题

时间:2016-11-07 21:16:22

标签: java constructor symbols

这两个文件的目的: 对象' na' (在第一个文件中)通过第二个文件调用函数来分析文本文件中的数字并返回最高数字,最低数字,总数和平均值。我经常收到错误"找不到符号"

文件1:

import java.io.*;    // Needed for IOException

/**
   This program demonstrates a solution to the
   Number Analysis Class programming challenge.
*/

public class NumberAnalyzerDemo
{
   public static void main(String[] args) throws IOException
   {
      // Create a NumberAnalyzer object.
      NumberAnalyzer na = new NumberAnalyzer("Numbers.txt");

      // Display data about the numbers in the file.
      System.out.println("The lowest number in the file is " +
                         na.getLowest());
      System.out.println("The highest number in the file is " +
                         na.getHighest());
      System.out.println("The total of the numbers in the file is " +
                         na.getTotal());
      System.out.println("The average of the numbers in the file is " +
                         na.getAverage());
   }
}

文件2:

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;

public class NumberAnalyzer
{           
    public NumberAnalyzer(String fileName)throws IOException
    {
        int count = 0;
        double total, lowest, highest, average;
        File f = new File("Numbers.txt");
        Scanner s = new Scanner(f);
        double[] numbers = new double[11];
    }

    public void getNumbers(String fileName)
    {   
        while(s.hasNextLine())
        {
            numbers[count] = s.nextDouble();
            count++;
        }
    }

    public int getNumberOfValues(String fileName)
    {
        return count;
    }

    public double getLowest()
    {
        double lowest = numbers[0]; // set the first number in array as tester

        for(int i = 0; i < count; i++)
        {
            if(numbers[i] < lowest)
            {
                lowest = numbers[i];
            }
        }
        return lowest;
    }

    public double getHighest()
    {
        double highest = numbers[0]; // set the first number in array as tester

        for(int i = 0; i < count; i++)
        {
            if(numbers[i] > highest)
            {
                highest = numbers[i];
            }
        }
        return highest;
    }

    public double getTotal()
    {   
        for(int i = 0; i < count; i++)
        {
            double total += numbers[i];
        }

        return total;
    }

    public double getAverage()
    {       
        for(int i = 0; i < count; i++)
        {
            double total += numbers[i];
        }

        double average = (total / count);
        return average;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;

public class NumberAnalyzer
{           
    File f;
    Scanner s;
    List<Double> numbers;

    public NumberAnalyzer(String fileName) throws IOException
    {
        f = new File("Numbers.txt");
        s = new Scanner(f);
        numbers = new ArrayList<Double>();
    }

    public void getNumbers(String fileName) throws Exception
    {   
        while(s.hasNextLine())
        {
            numbers.add(s.nextDouble());
        }

      if (numbers.size() < 1) {
        throw new Exception("Empty file or wrong path exception");
      }
    }

    public int getNumberOfValues(String fileName)
    {
        return numbers.size();
    }

    public Double getLowest()
    {
        Double lowest = numbers.get(0); // set the first number in array as tester

        for(int i = 0; i < numbers.size(); i++)
        {
            if(numbers.get(i) < lowest)
            {
                lowest = numbers.get(i);
            }
        }
        return lowest;
    }

    public Double getHighest()
    {
        Double highest = numbers.get(0); // set the first number in array as tester

        for(int i = 0; i < numbers.size(); i++)
        {
            if(numbers.get(i) > highest)
            {
                highest = numbers.get(i);
            }
        }
        return highest;
    }

    public Double getTotal()
    {   
        Double total = 0d;

        for(int i = 0; i < numbers.size(); i++)
        {
            total += numbers.get(i);
        }

        return total;
    }

    public Double getAverage()
    {       
        Double total = 0d;
        for(int i = 0; i < numbers.size(); i++)
        {
            total += numbers.get(i);
        }

        Double average = (total / numbers.size());
        return average;
    }
}

您在构造函数的范围内声明变量而不是在类中,因此方法无法访问这些变量。