简体   繁体   English

Java扫描仪/阵列金字塔

[英]Java Scanner/Array Pyramid

So I'm trying to make a pyramid of added values. 所以我正在努力使附加值金字塔化。 Ex. 例如 Line 1 has (5), Line 2 has (6,7) Line 3 has (12,10,7). 第1行具有(5),第2行具有(6,7)第3行具有(12,10,7)。 The goal is to add the highest value of the first line, with the highest connecting child value of the next line. 目标是将第一行的最大值与下一行的最高连接子级值相加。 So in this case, you would add 5+7+10, giving you a result of 22. The reason why you can't use the 12 in line 3 is because you have to use a child of the number above (Each parent has 2 children). 因此,在这种情况下,您将添加5 + 7 + 10,结果为22。之所以不能在第3行中使用12,是因为您必须使用上述数字的子代(每个父代都有2个孩子)。

My approach is to use Scanner to load int values into an array line-by-line, and somehow index the position of the previous line's highest child value to add it to a running total. 我的方法是使用Scanner逐行将int值加载到数组中,并以某种方式索引上一行的最高子值的位置,以将其添加到运行总计中。 Here's the code I have so far... 这是我到目前为止的代码...

//in the data file... //在数据文件中...

5 5

6 7 6 7

12 10 7 12 10 7

//that's all. //就这样。

    public static void main(String[] args) {
    Scanner scanner = null;
    try {
        scanner = new Scanner(new File("/users/joe/desktop/data.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
        e.printStackTrace();

    } //reads the file

    int[] a = new int[100]; //establishes new array with a max size of 100
    int i = 0; //placeholder for array position
    int result = 0;
    int total = 0;

    while(scanner.hasNextLine()){ //loops through line
        a[i++] = scanner.nextInt(); //adds int to array
        if(i == 0){ //does this belong here?
            result = a[0];
        }
        else{
            if(a[i-1] >= a[i+1]){
                result = a[i-1];
            }
            else if(a[i-1] <= a[i+1]){
                result = a[i+1];
            }
        }
    }
    total =  total + result;
    scanner.close();

    System.out.print(Arrays.toString(a));
    System.out.println("\n" + total);


    }
    }

Currently, this will print out: [5,6,7,12,10,7,0,0,0,0,0,...up to 100 positions] 当前,它将打印出:[5,6,7,12,10,7,0,0,0,0,0,...最多100个位置]

5 5

How can I get the Scanner to read one line, load it into an array, loop that, and save the highest child value from the next line's array? 如何使扫描仪读取一行,将其加载到一个数组中,对其进行循环,并保存下一行数组中的最高子级值?

When I execute that code, I get a NoSuchElementException after reading in all the numbers. 当我执行该代码时,在读取所有数字后会得到NoSuchElementException。 This occurs because you are always checking for the next line of the file, but reading in the next integer. 发生这种情况是因为您始终在检查文件的下一行,但要读下一个整数。

There are so many logic flaws in the code that its hard to comment on them all. 代码中存在太多逻辑缺陷,因此很难一一列举。 Here's a few that you could work on: 您可以从事以下工作:

You only add total + result ONCE, outside of your loop. 您只需要在循环外添加合计+结果一次。 You should move that inside a loop so total is calculated properly 您应该将其移入循环,以便正确计算总数

You calculate result incorrectly. 您计算结果不正确。 At the very least, you should have a variable indicating the position of the highest number on the last row. 至少,您应该有一个变量,该变量指示最高数字在最后一行的位置。 You also are comparing i -1 with i+1... this won't work when you get down to the last row, as you'll be comparing items to either side of i rather than items below i. 您还正在将i -1与i + 1进行比较...当您下到最后一行时,这将不起作用,因为您将比较i两侧的项目,而不是i以下的项目。

I'd recommend reading the variables into a triangular 2D array. 我建议将变量读入三角形2D数组。 You can do this by creating another scanner for each line; 您可以通过为每行创建另一个扫描仪来实现。 reading each line into the new scanner; 将每一行读入新的扫描仪; and polling for nextInt(). 并轮询nextInt()。 This then lets you compare arr[row][pos] and arr[row][pos+1] which will be the children of arr[row-1][pos]. 然后,您可以比较arr [row] [pos]和arr [row] [pos + 1],它们将是arr [row-1] [pos]的子代。

Once you can read the file into a 2D array and print int out from the array to look just like the file contents, and if you're still having trouble, come back and I can give you some more help. 一旦您可以将文件读取到2D数组中并从数组中打印出int的外观就像文件内容一样,并且如果仍然遇到问题,请回来,我们可以为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM