英文:
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 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 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 of0
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, incrementingend
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 of0
occurs. - Once the threshold is met, simply continue reading until a depth of
0
is found or until the end of file is reached, incrementingend
as appropriate
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();
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论