英文:
trouble printing a range of numbers in an array list with own inputs and indices - 3.8 MOOC JAVA
问题
我在 MOOC.fi 上遇到了一个练习问题,我卡住了!我们正在学习 Java 中的数组和列表,以下是问题的文本:
"练习模板包含一个从用户那里读取数字并将其添加到列表中的基础部分。一旦用户输入数字 -1,读取就会停止。
在程序完成要求输入数字后,扩展该程序以要求输入起始和结束索引。之后,程序应打印出列表中在指定范围内的所有数字(介于用户给出的索引之间,包括边界)。您可以假设用户给出与列表中某些数字匹配的索引。"说明还强调要使用列表和索引。
以下是我的代码:
import java.util.ArrayList;
import java.util.Scanner;
public class OnlyTheseNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int number = Integer.valueOf(scanner.nextLine());
if (number == -1) {
break;
}
numbers.add(number);
}
System.out.println("从哪里开始? ");
int first = Integer.valueOf(scanner.nextLine()); // 输入起始索引
System.out.println("到哪里? ");
int last = Integer.valueOf(scanner.nextLine()); // 输入结束索引
for (int i = first; i <= last; i++) {
System.out.println(numbers.get(i));
}
}
}
结果 - 如果我使用以下输入数字:
3
2
1
4
7
-1
从哪里开始?
2
到哪里?
4
则输出如下:
2
1
4
在打印输入范围内的数字时,您需要使用循环变量 i
作为索引来获取列表中的数字,然后使用 numbers.get(i)
来访问列表中的元素。这样就可以打印出指定范围内的数字了。
英文:
I have an exercise problem from MOOC.fi that I am stuck on! We are learning about arrays and lists in Java, and here is the text for the problem:
"The exercise template contains a base that reads numbers from the user and adds them to a list. Reading is stopped once the user enters the number -1.
Expand the program to ask for a start and end indices once it has finished asking for numbers. After this the program shall prints all the numbers in the list that fall in the specified range (between the indices given by the user, inclusive). You may assume that the user gives indices that match some numbers in the list." The instructions also emphasize using lists and indices.
Here is my code:
import java.util.ArrayList;
import java.util.Scanner;
public class OnlyTheseNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int number = Integer.valueOf(scanner.nextLine());
if (number == -1) {
break;
}
numbers.add(number);
}
System.out.println("From where? ");
int first = Integer.valueOf(scanner.nextLine()); //first index input to
System.out.println("To where? ");
int last = Integer.valueOf(scanner.nextLine()); // last index input in a list
for (int i = numbers.get(first); i <= numbers.get(last); i++) {
System.out.println(numbers);
}
/*int i = numbers.get(first);
while (i <= numbers.get(last)) {
System.out.println(arr[numbers]);
i++;
}*/
}
}
Result - If I use input numbers:
3
2
1
4
7
-1
From where?
2
To where?
4
[3, 2, 1, 4, 7]
[3, 2, 1, 4, 7]
[3, 2, 1, 4, 7]
[3, 2, 1, 4, 7]
[3, 2, 1, 4, 7]
[3, 2, 1, 4, 7]
[3, 2, 1, 4, 7]
It works up until it needs to print the range of inputs, between the first and last indices, I have tried using i instead of numbers in the System.out.println(numbers);, but that prints the first input number to the last input number. I've tried both a while loop and a for loop, but am stuck.
This is my first post, I apologize if it is bad formatting or too much text!
答案1
得分: 0
如果你使用 Java 8 及以上版本:
System.out.println(IntStream.rangeClosed(first, last).mapToObj(number::get).collect(Collectors.toList()));
如果你不想使用流(stream):
for (int i = first; i <= last; i++) {
System.out.println(numbers.get(i));
}
英文:
if you use java 8 and above:
System.out.println(IntStream.rangeClosed(first, last).mapToObj(number::get).collect(Collectors.toList()));
if you dont want to use a stream:
for (int i = first; i <= last; i++) {
System.out.println(numbers.get(i));
}
答案2
得分: 0
只需将您的for循环替换为以下内容:
for (int i = first; i < last; i++) {
System.out.println(numbers.get(i));
}
英文:
just replace your for loop with this:
for (int i = first; i < last; i++) {
System.out.println(numbers.get(i));
}
答案3
得分: 0
尝试理解你所编写的代码。你的for循环表明它从存储在index2中的元素1初始化,将在存储在index4中的元素7处停止。
现在这意味着
for (int i = numbers.get(first); i <= numbers.get(last); i++) {
System.out.println(numbers);
}
这将间接地表示
for (int i = 1; i <= 7; i++) {
System.out.println(numbers);
}
这就是为什么你的代码会将数组打印出7次。
for (int i = first; i <= last; i++) {
System.out.println(num.get(i));
}
英文:
Try to understand the code that you have written. Your for loop indicates that it initializes from the element 1 that is stored in index2 and will stop at element 7 that is stored in index4
now that means
for (int i = numbers.get(first); i <=numbers.get(last); i++) {
System.out.println(numbers);
}
this will indirectly say
for( int i =1;i<=7;i++){
System.out.println(numbers);}
that is why your code is printing the array 7 times.
for (int i =first; i <= last; i++) {
System.out.println(num.get(i));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论