-2

文本文件(myfile.txt)的编号如下:

6
19
20
23
34
45
47
51
54
56
61
70
72
76
90
93
94
96
105
111

我可以用 Java 读取和打印这个文本文件作为字符串标记,我的代码是:

                      File fileName = new File("myfile.txt");         
                      Scanner inFile = new Scanner(fileName);        
                      while (inFile.hasNext())         
                      {         
                      token = inFile.next( ); 

                      System.out.println(token);

                      } 

                    inFile.close();

现在我怎样才能在java中得到这些数字的总和?我的意思是总和(6+19+20+23+..........+111)

4

4 回答 4

3

使用int变量sum,并使用scanner.nextInt()来提取数字。并在每次迭代中添加。

                      File fileName = new File("myfile.txt");         
                      Scanner inFile = new Scanner(fileName);
                       int sum=0;        
                      while (inFile.hasNext())         
                      {         
                           sum+= inFile.nextInt( );     
                      } 
                      System.out.println(sum);

                    inFile.close();
于 2012-11-18T19:40:53.153 回答
1

您可以使用Integer.parseInt方法将字符串转换为 int。然后将其添加到sum您需要在 while 之外声明的变量中。

Scanner inFile = new Scanner(fileName);        
int sum = 0;
while (inFile.hasNext()) {         
    token = inFile.next( ); 

    System.out.println(token);
    sum += Integer.parseInt(token);

} 

inFile.nextInt()好吧,如果您正在读取整数值,那么 using是一个更好的选择。并inFile.hasNextInt()在您的 while 循环中使用。

于 2012-11-18T19:40:53.027 回答
0

nextInt()首先将使用ofScanner类的整数读入 a Listfor然后使用“增强”循环将这些数字中的每一个添加到列表中

于 2012-11-18T19:41:57.270 回答
0

所以我能够找出我的问题并且成功了,但我仍然希望任何人都可以输入是否以及如何通过使用三元运算符来简化此语法。

import java.util.Scanner;
import java.io.*;
public class Midterm
{
    public static void main(String[]args) throws Exception
    {
     final String FILE = "integers.txt";
     final String RELATIVE_DIRECTORY_FILE = "integers.txt ";
    int positiveNumberCount = 0,
    negativeNumberCount = 0;
    String dataItem;
    double sum = 0;
    double sumForAverage = 0;
    double average = 0;

    File inputFile = new File(RELATIVE_DIRECTORY_FILE);
    if(!inputFile.exists())
    {
        System.out.printf("%n%s%s%s", "The file, ",   RELATIVE_DIRECTORY_FILE, " does not exist");
    }

    if(inputFile.exists())
    {
        System.out.printf("%s%s%s", "The file ", RELATIVE_DIRECTORY_FILE,       "was found");
        Scanner file = new Scanner(inputFile);
        //***if i need to use a delimiter *****
        //file.useDelimiter(",") where in between " "  is my type of delimiter
        while(file.hasNext())
        {



            dataItem = file.nextLine();

            double numericalDataItem = Double.parseDouble(dataItem);

            boolean isPositive = false;
            boolean isNegative = false;

            if(numericalDataItem > 0)
            {
                isPositive = true;
                positiveNumberCount++;
            }
            else if(numericalDataItem < 0)
            {
                isNegative = true;
                negativeNumberCount++;
            }

            sum += Double.parseDouble(dataItem);
            sumForAverage = positiveNumberCount + negativeNumberCount;
            average = sum/sumForAverage;

        } 
        System.out.printf("%n%s%d%n%s%d%n%s%.2f%n%s%.2f", "Positive Number Count: ", positiveNumberCount,
        "Negative Number Count: ", negativeNumberCount, "Sum Of Numbers: ", sum, "Average of Numbers: ", average);
    }
}
}
于 2016-10-21T00:03:09.587 回答