Read Data From Text File And Sum Numbers

前端 未结 8 1488
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 13:59

I want to read in data from a text file which is full of integers and have the program print those integers out to the screen while summing them. This shouldn\'t be hard, b

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-21 14:29

    You are calling textfile.nextInt() twice in the loop. Try:

    static void filereader(Scanner textfile)     
    {         
        int i = 0;         
        int sum = 0;          
        while(i <= 19)         
        {       
            int nextInt = textfile.nextInt();          
    
            System.out.println(nextInt);             
            sum = sum + nextInt;
            i++;         
        }     
    }
    

提交回复
热议问题