如何使用for循环来解决这个任务?

huangapple go评论85阅读模式
英文:

How Can I use for-loop to solve this task?

问题

I have a txt file which contains only integers. Each line contains 1 integer.
The txt file is representing pits and their depth in the ground:
如何使用for循环来解决这个任务?

For example:
0
0
0
1
2
2
2
2
2
2
3
2
2
1
0

In this case, the pit starts from the index 3 and ends at index 13.
Lets say the user input is 10. The depth of the pit is 2m at index 10.
From the 10. index I need to go backwards until I reach the index with value of 0 to determine the beginning of the pit. After I need to go forward until I reach 0 again to determine the end of the pit.

英文:

I have a txt file which contains only integers. Each line contains 1 integer.
The txt file is representing pits and their depth in the ground:
如何使用for循环来解决这个任务?

For example:
0
0
0
1
2
2
2
2
2
2
3
2
2
1
0

In this case, the pit starts from the index 3 and ends at index 13.
Lets say the user input is 10. The depth of the pit is 2m at index 10.
From the 10. index I need to go backwards until I reach the index with value of 0 to determinate the beginig of the pit. After I need to go forward until I reach 0 again to determinate the end of the pit.
How would you go about this?

int in = sc.nextInt();
int input = Integer.parseInt(data.get(in - 1));
if (input == 0) {
    System.out.println("There is no pit at this index!");
}

for (int i = input; i < data.size(); i--) {
    if (Integer.parseInt(data.get(i)) == 0) {
        System.out.println("The first index of the pit : " + data.get(i + 1));
    }
}

答案1

得分: -1

Upon thinking about this some, I came up with an improved solution which does not require storage of the values nor does it require multiple loops.
这部分内容提到,我考虑了一些,想出了一个改进的解决方案,不需要存储值,也不需要多次循环。

This presumes that the first element in the file is the starting position (threshold) and what follows are the depths. It is up to you to make adjustments to suit your situation.
这假定文件中的第一个元素是起始位置(threshold),其后是深度。您可以根据您的情况进行调整。

  • The basic idea is to read in the values until the threshold is reached, only changing start if a depth of 0 occurs.
    基本思路是读取值直到达到阈值,仅在出现深度为 0 时才更改 start

  • Once the threshold is met, simply continue reading until a depth of 0 is found or until the end of file is reached, incrementing end as appropriate.
    一旦达到阈值,只需继续读取,直到找到深度为 0 或达到文件末尾,适当地增加 end

String fileName = "<your filename>";
try (Scanner in = new Scanner(new File(fileName))) {
    int threshold = in.nextInt();
    int index = 0;
    int end = 0;
    int start = 0;
    while (in.hasNextInt()) {
       int depth = in.nextInt();
       if (depth == 0 && index < threshold) {
           start = index+1;
       }
       if (index >= threshold && depth == 0) {
          break;
       }
       end = index;
       index++;
    }
    if (end < threshold) {
        System.out.printf("No pit at index %d%n", threshold);
    } else {
        System.out.printf("Pit starts at index %d and ends at index %d%n",
                start, end);
    }
} catch (FileNotFoundException fne) {
    fne.printStackTrace();
}

请注意:这是您提供的代码的翻译部分。如果需要更多信息或帮助,请告诉我。

英文:

Upon thinking about this some, I came up with an improved solution which does not require storage of the values nor does it require multiple loops.
This presumes that the first element in the file is the starting position (threshold) and what follows are the depths. It is up to you to make adjustments to suit your situation.

  • The basic idea is to read in the values until the threshold is reached, only changing start if a depth of 0 occurs.
  • Once the threshold is met, simply continue reading until a depth of 0 is found or until the end of file is reached, incrementing end as appropriate
String fileName = &quot;&lt;your filename&gt;&quot;;
try (Scanner in = new Scanner(new File(fileName))) {
    int threshold = in.nextInt();
    int index = 0;
    int end = 0;
    int start = 0;
    while (in.hasNextInt()) {
       int depth = in.nextInt();
       if (depth == 0 &amp;&amp; index &lt; threshold) {
           start = index+1;
       }
       if (index &gt;= threshold &amp;&amp; depth == 0) {
          break;
       }
       end = index;
       index++;
    }
    if (end &lt; threshold) {
        System.out.printf(&quot;No pit at index %d%n&quot;, threshold);
    } else {
        System.out.printf(&quot;Pit starts at index %d and ends at index %d%n&quot;,
                start, end);
    }
} catch (FileNotFoundException fne) {
    fne.printStackTrace();
}

</details>



huangapple
  • 本文由 发表于 2023年6月29日 20:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定