遍历数组JAVA中的.txt文件

时间:2018-09-02 22:54:46

标签: java arrays algorithm data-structures text-files

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类。有没有更简单的方法来运行此文件?

3 个答案:

答案 0 :(得分:0)

您可以使用

BufferedReader input = new BufferedReader(new FileReader("input.txt"));

获取输入。 Java的控制台未称为StdOut,因此编译器会告诉您创建该类。要打印到Java控制台,可以使用

System.out.println(a);

答案 1 :(得分:0)

似乎您正在学习普林斯顿大学的编程课程。在这种情况下,您需要仔细阅读说明。

在此页面https://introcs.cs.princeton.edu/java/stdlib/上,您可以找到说明:

  

Classpath。using UnityEngine; public class LightDrone : MonoBehaviour { public float speed = 60.0f; // Our destination needs to be remembered outside a single iteration of // Update. So we put it outside of the method in order to remember it // across multiple frames. private Vector3 currentDestination; // We need to check if we're at the destination yet so we know when to stop. private bool notAtDestinationYet; // When we're closer to the destination than this tolerance, we decide that // we have arrived there. private float tolerance = 0.1f; private void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { var newPosition = hit.point; // You can add whatever height you want here. I added // just 2 instead of 10. currentDestination = newPosition + new Vector3(0, 2.0f, 0); notAtDestinationYet = true; } } if (notAtDestinationYet) { // Use a bit of vector math to get the direction from our current // position to the destination. The direction is a normalized Vector // that we can just multiply with our speed to go in that direction // at that specific speed! var heading = currentDestination - transform.position; var distance = heading.magnitude; var direction = heading / distance; // Check if we've arrived at our destination. if (distance < tolerance) { notAtDestinationYet = false; } else { // If the remaining distance between us and the destination is // smaller than our speed, then we'll go further than necessary! // This is called overshoot. So we need to make our speed // smaller than the remaining distance. // We also multiply by deltaTime to account for the variable // amount of time that has passed since the last Update() call. // Without multiplying with the amount of time that has passed // our object's speed will increase or decrease when the // framerate changes! We really don't want that. float currentSpeed = Mathf.Clamp(speed * Time.deltaTime, Mathf.Epsilon, distance); transform.position += direction * currentSpeed; } } } } 与您正在编写的程序放在同一目录中(但不要解压缩它)。然后,编译并执行为   如下:

     

OS X / Linux

stdlib.jar
     

Windows

% javac -cp .:stdlib.jar MyProgram.java
% java  -cp .:stdlib.jar MyProgram

stdlib.jar包含类% javac -cp .;stdlib.jar MyProgram.java % java -cp .;stdlib.jar MyProgram In等的.java和.class文件。

答案 2 :(得分:0)

这将逐行读取您的文件

public static void main(String[] args) throws IOException {
    Path p = Paths.get("/Users/ay/Desktop/tx1.txt");
    try(Stream<String> lins = Files.lines(p)) {
        lins.forEach(System.out::println);
    }
}