2015-07-28 22 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"); 
     } 
    } 
} 

}