在JAVA中列表中数字的索引

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

Index of number(s) in list in JAVA

问题

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class IndexOf {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. ArrayList<Integer> list = new ArrayList<>();
  7. while (true) {
  8. int input = Integer.valueOf(scanner.nextLine());
  9. if (input == -1) {
  10. break;
  11. }
  12. list.add(input);
  13. }
  14. System.out.println("Search for? ");
  15. int src = scanner.nextInt();
  16. for (int i = 0; i < list.size(); i++) {
  17. int num = list.get(i);
  18. if (src == num) {
  19. System.out.println(src + " is at index " + i);
  20. }
  21. }
  22. }
  23. }
英文:

I need to have a program that asks the user for a number, and reports that number's index in the list. If the number is not found, the program should not print anything.

Example:

  1. Sample Output:
  2. 1
  3. 2
  4. 3
  5. 3
  6. 4
  7. Search for? 3
  8. 3 is at index 2
  9. 3 is at index 3

This is what I have written but the put is looping multiple times. Can you suggest fixing it?

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class IndexOf {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
  7. while (true) {
  8. int input = Integer.valueOf(scanner.nextLine());
  9. if (input == -1) {
  10. break;
  11. }
  12. list.add(input);
  13. }
  14. System.out.println(&quot;Search for? &quot;);
  15. int src = scanner.nextInt();
  16. int ind = 0;
  17. for(int i=0; i&lt;list.size(); i++){
  18. int num = list.get(i);
  19. if(src == num){
  20. ind = list.indexOf(src);
  21. }
  22. System.out.println(src + &quot; is at index &quot; + ind);
  23. }
  24. }
  25. }

EDIT

> Input: 1 2 3 3 4 -1
>
> Search for? 3 Output: 3 is at index 0 3 is at index 0 3 is at index 2 3 is at index 2 3 is at index 2 ///So it must be only one sentence for each index. Even if I put after "for" loop, it only output the first if index.

答案1

得分: 0

我能看到你代码中的一个问题,即你打印了所有的内容,而不仅仅是匹配的部分。为了解决这个问题,你需要将System.out.println放到if语句中,就像这样:

  1. for(int i=0; i<list.size(); i++){
  2. int num = list.get(i);
  3. if(src == num){
  4. ind = i;
  5. System.out.println(src + " 在索引 " + ind);
  6. }
  7. }

如果还有其他问题,请告诉我。

英文:

I can see a single problem in your code, that is, you print everything, not only the matches. To solve that you need to put your System.out.println into the if, like this:

  1. for(int i=0; i&lt;list.size(); i++){
  2. int num = list.get(i);
  3. if(src == num){
  4. ind = i;
  5. System.out.println(src + &quot; is at index &quot; + ind);
  6. }
  7. }

Let me know if anything else was wrong.

huangapple
  • 本文由 发表于 2023年2月9日 00:42:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388979.html
匿名

发表评论

匿名网友

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

确定