猿问

循环遍历数组 JAVA 中的 .txt 文件

public class ThreeSum {


public static int count(int[] a) {

    int n = a.length;

    int count = 0;

    for (int i = 0; i < n; i++) {

        for (int j = i+1; j < n; j++) {

            for (int k = j+1; k < n; k++) {

                if (a[i] + a[j] + a[k] == 0) {

                    count++;

                }

            }

        }

    }

    return count;



public static void main(String[] args)  { 


    In input = new In("input.txt");

    int [] a = input.readInts(args[0]);

    StdOut.println(count(a));

} } 

这是我的代码。我正在尝试读取包含随机数的文本文件,但每次运行它时,我都会说要创建类 In 和类 StdOut。有没有更简单的方法来运行这个文件?


吃鸡游戏
浏览 250回答 3
3回答

千巷猫影

您可以使用BufferedReader&nbsp;input&nbsp;=&nbsp;new&nbsp;BufferedReader(new&nbsp;FileReader("input.txt"));获取输入。Java 的控制台不称为 StdOut,因此编译器会告诉您创建该类。要打印到 Java 控制台,您可以使用System.out.println(a);

互换的青春

这将逐行读取您的文件public static void main(String[] args) throws IOException {&nbsp; &nbsp; Path p = Paths.get("/Users/ay/Desktop/tx1.txt");&nbsp; &nbsp; try(Stream<String> lins = Files.lines(p)) {&nbsp; &nbsp; &nbsp; &nbsp; lins.forEach(System.out::println);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答