简体   繁体   English

如何显示数组的位置

[英]how to display the position of array

Your program should display whether the inputted integer value is found in list, how many of it is in list, and what are their locations in list? 您的程序应显示是否在列表中找到了输入的整数值,列表中有多少个整数以及它们在列表中的位置是什么? sample 样品

List : 1 3 2 5 7 8 5 6 9 4

Input value to search in List: 9
The value 9 is in List!
There are 4 of it in List.
Located at: list[4], list[5], list[6], list[8]

this is my code 这是我的代码

import java.io.*; 
public class List{ 
  public static void main(String[] args){ 
    int list[] = new int[10]; 
    int i, num = 0, num1=0; 
    String input = " "; 
    BufferedReader in = new BufferedReader(new 
                               InputStreamReader(System.in)); 

    for(i = 0; i < 10; i++){ 
      list[i] = 0; 
    } 
    for(i = 0; i < 10; i++){ 
      System.out.print("Input value for list[" + i + "] = "); 
      try{ 
        input = in.readLine();         
        num = Integer.parseInt(input); 
        list[i] = num; 
      for(i = 0; i < 10; i++){ 
        System.out.println("list[" + i + "] = " +list[i]); 
      } 
      for (int b = 0; b < list.length; ++b) {
        if (num1 == list[b]) {
          returnvalue = b;
          break;
        }
      }
      System.out.print("Input value for list");
      input = in.readLine();
      num1 = Integer.parseInt(input);
    }catch(IOException e){} 
      System.out.println("the position is" + returnvalue);
    }
  } 
} 

I think the position that is returning is not accurate can you help me? 我认为返回的职位不准确,您能帮我吗?

This will do what you want... Modify the System.out.println(); 这将完成您想要的操作。修改System.out.println(); part according to your need. 根据您的需要分开。

public static void main(String[] args) throws IOException {
int list[] = new int[10];
int i, num = 0, num1 = 0;
int returnvalue = 0;
String input = " ";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

for (i = 0; i < 10; i++) {
    list[i] = 0;
    }
for (i = 0; i < 10; i++) {
    System.out.print("Input value for list[" + i + "] = ");    
    input = in.readLine();
    num = Integer.parseInt(input);
    list[i] = num;
    }

for (i = 0; i < 10; i++) {
    System.out.println("list[" + i + "] = " + list[i]);
    }
System.out.print("Input value for checking in  list ");
input = in.readLine();
num1 = Integer.parseInt(input);

boolean flag = false;
int[] arr = new int[10];
int count= 0;
for (int b = 0; b < list.length; ++b) {
  if (num1 == list[b]) {              
      flag = true;
      arr[count]=b;
      count++;
      }
}
if(flag)
{
   System.out.println("The value "+num1+" is in List!");
   System.out.println("There are "+count+" of it in List");
   System.out.print("Located at: ");
   for(int j=0; j<count;j++)
   {
    System.out.print(" List["+arr[j]+"]");
   }
} 
else {
System.out.print(" Element Not found");
}

} }

The Console Part for input output is... 输入输出的控制台部分是...

Input value for list[0] = 4
Input value for list[1] = 5
Input value for list[2] = 6
Input value for list[3] = 4
Input value for list[4] = 5
Input value for list[5] = 6
Input value for list[6] = 5
Input value for list[7] = 4
Input value for list[8] = 4
Input value for list[9] = 6
list[0] = 4
list[1] = 5
list[2] = 6
list[3] = 4
list[4] = 5
list[5] = 6
list[6] = 5
list[7] = 4
list[8] = 4
list[9] = 6
Input value for checking in  list 4
The value 4 is in List!
There are 4 of it in List.
Located at:List[0] List[3] List[7] List[8]

Fixes in your code 修正您的代码

  1. A try block is either with a catch block or with a finally block or both. 尝试块可以是catch块,也可以是finally块,或者两者都可以。
  2. Having two same variables in inner and outer loop is not good. 内部和外部循环中具有两个相同的变量不是很好。 In your case the outer loop will iterate just once. 在您的情况下,外循环将仅迭代一次。 I think which is not needed. 我认为不需要。

There are two nested for loop where the loop control variable is same: 有两个嵌套的for循环,其中循环控制变量相同:

for(i = 0; i < 10; i++){ 
    System.out.print("Input value for list[" + i + "] = "); 
    ... 
    for(i = 0; i < 10; i++){
         System.out.println("list[" + i + "] = " +list[i]);
    }
    ...

So, the outer for loop will never iterate what you are expecting. 因此,外部for循环将永远不会迭代您所期望的。 Use a different loop control variable for the inside for loop. 在内部for循环中使用其他循环控制变量。

here is my solution for your problem. 这是我为您解决的问题。 no need to use that much of loops. 无需使用那么多循环。 you want to use only one foreach - How does the Java 'for each' loop work? 您只想使用一个foreach - Java“ for each”循环如何工作? (if you don't need to ask question again and agian). (如果您不需要再次询问问题和咨询)。 I was unable to test this lot :). 我无法测试很多:)。 But, I hope this will work. 但是,我希望这会起作用。

import java.util.*;
class MyList{
public static void main(String arga[]){
    int array[] = {1,3,2,5,7,8,5,6,9,4};

    Scanner input = new Scanner(System.in);
    // recursively ask the question
    while(true){
        System.out.print("Enter the value to search in list: ");

        int value = input.nextInt();
        System.out.println();
        int i = 0;// to get the array index
        int count = 0; // to get how many 

        String list = "Located at: ";
        boolean isTrue = false; // to get entered value is in the list or not
        for(int a: array){
            if(a == value){
                isTrue = true;
                list += "list[" + i + "],";
                count++;
            }
            i++;
        }
        if(isTrue){
            //remove the last "," from the string
            list = list.substring(0, list.length() - 1);
            System.out.println("The value " + value +" is in List!");
            System.out.println("There are " + count +" of it in List.");
            System.out.println(list);
        }else{
            System.out.println("this value is not in the list");
        }
    }
}

} }

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

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