如何显示数组的位置

时间:2015-07-28 10:32:54

标签: java arrays

您的程序应显示输入的整数值是否在列表中找到,列表中有多少是什么,以及它们在列表中的位置是什么? 样品

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]

这是我的代码

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);
    }
  } 
} 

我认为回归的位置不准确你能帮帮我吗?

3 个答案:

答案 0 :(得分:0)

这将按您的要求进行操作...根据您的需要修改System.out.println();部分。

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");
}

}

输入输出的控制台部分是......

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] 

修复了代码

  1. try块可以是catch块,也可以是finally块或两者。
  2. 在内循环和外循环中有两个相同的变量并不好。在您的情况下,外部循环将只迭代一次。我认为哪些不需要。

答案 1 :(得分:0)

有两个嵌套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]);
    }
    ...

因此,外部for循环永远不会迭代你期望的东西。对内部for循环使用不同的循环控制变量。

答案 2 :(得分:0)

这是我的问题解决方案。不需要使用那么多的循环。你只想使用一个foreach - How does the Java 'for each' loop work?(如果你不需要再问问题和agian)。我无法测试这个很多:)。但是,我希望这会奏效。

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");
        }
    }
}

}