0

我想在文本文件中输出一些数字的总数,并使用 anArrayList来寻找它们等,我不确定我会怎么做?这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListOfNumbers {

    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<String> numbers = new ArrayList<String>();
        Scanner Scan = new Scanner (new File("numbers.txt"));

        while (Scan.hasNext()){
            numbers.add(Scan.nextLine());
        }
        for(int i = 0; i < numbers.size(); i++){ 
            System.out.println(numbers.get(i));
        }

        System.out.println("")

        Scan.close();
    }

}
4

1 回答 1

0
public static void main(String[] args) throws FileNotFoundException {
        Scanner Scan = new Scanner (new File("numbers.txt"));
        double sum = 0;
        while (Scan.hasNext()){
            sum += Double.parseDouble(Scan.nextLine());
        }

        System.out.println(sum);

        Scan.close();
    }
于 2013-01-10T19:27:46.833 回答