Java - 数组不从文本文件中读取数据 [英] Java - array not reading data from a text file

查看:83
本文介绍了Java - 数组不从文本文件中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10个整数的文本文件,1 2 3 4 5 6 7 8 9 10.它们按顺序打印出来然后我试图找到文本文件中整数的最小,最大和平均值。



我尝试过:



I have a text file with 10 integers, 1 2 3 4 5 6 7 8 9 10. They print out in order however I am trying to find the smallest, largest and average out of the integers in the text file.

What I have tried:

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

public class Exercise2txt {

	public static void main(String[] args) throws Exception {

		int[] numbers = new int[10];

		int largest = Integer.MIN_VALUE;
		int smallest = Integer.MAX_VALUE;
		double total = 0;
		double avg;
		int i = 0;

		
			Scanner inFile = new Scanner(new File("numbers.txt"));

			for (int y = 0; y < 10; y++) 
				System.out.println(inFile.nextInt());
			
			if (numbers[i] > largest) {
				largest = numbers[i];
			}

			if (numbers[i] < smallest) {
				smallest = numbers[i];
			}

			for (int x = 1; x < numbers.length; x++) {
				total += numbers[x];
			}

			avg = total / numbers.length;
		

		System.out.println("Largest number = " + largest);
		System.out.println("Smallest number = " + smallest);
		System.out.println("The average of all numbers = " + avg);

	}
}





这是我目前的代码



This is the code i have at the moment

推荐答案

Quote:

按顺序打印



这正是你在代码中所做的。


This is exactly what you do in your code.

System.out.println(inFile.nextInt());





此行正在读取文件和打印,但保存整数以备将来使用的魔力在哪里?



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器上逐步运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你显示你要发生什么来帮助你 上。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较它应该这样做。



This line is reading from file and printing, but where is the magic that save the integer for future use ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


你的代码几乎完成,你只需要正确读取文件。尝试

Your code is almost complete, you've just to properly read the file. Try
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Exercise2txt {

  public static void main(String[] args) throws Exception {

    int[] numbers = new int[10];

    int largest = Integer.MIN_VALUE;
    int smallest = Integer.MAX_VALUE;
    int count = 0;
    double sum = 0.0;
    double avg;
    Scanner inFile = new Scanner(new File("numbers.txt"));
    while ( inFile.hasNext())
    {
      int n = inFile.nextInt();
      if ( largest < n)
        largest = n; 
      if (smallest > n)
        smallest = n;
      sum += n;
      ++count;
    }
    avg = sum / count;

    System.out.println("Largest number = " + largest);
    System.out.println("Smallest number = " + smallest); 
    System.out.println("The average of all numbers = " + avg);
  }
}


这篇关于Java - 数组不从文本文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆