2015-07-28 38 views
2

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

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

我认为这是返回的位置是不准确的,你能帮帮我吗?

+1

为什么你这么认为吗?这段代码运行了吗?输入和输出是什么? – moffeltje

+0

,因为答案总是小于1 \ – Hello23

+0

什么是'returnvalue'? –

回答

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

我该如何显示?位于:列表[4],列表[5],列表[6],列表[8] – Hello23

+0

根据您的要求修改代码 – CoderNeji

+0

值9显示在列表中! 列表中有4个这是怎么回事? – Hello23

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循环中使用不同的循环控制变量。

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

}