2014-05-04 190 views
-2

我一直停留在這個計劃了一段時間搜索字符串我必須做出一個程序,當用戶輸入的項目和門店數量將會給量在二維數組

import java.util.Scanner; 


public class Searching { 

    public static void main(String[] args) { 
     String firstarray[][] = { 
       {"", "Store101", "Store102", "Store103", "Store104"}, 
       {"Tennis Shoes", "102", "54", "20", "78"}, 
       {"Sweaters", "45", "25", "35", "75"}, 
       {"Jeans", "12", "35", "45", "65"}, 
       {"Shorts", "54", "25", "34", "45"}, 
       {"Jackets", "15", "35", "50", "25"} 
     }; 

     Scanner input = new Scanner(System.in); 
     System.out.println("Enter item"); 
     String userintake = input.next(); 
     System.out.println("Enter Store Number"); 
     String store = input.next(); 

     searchingitem(firstarray, userintake, store); 

    } 

    private static void searchingitem(String x[][], String item, String place) { 
     for (int i = 0; i < x.length; i++) { 
      for (int j = 0; j < x[i].length; j++) { 
       if ((item.equals(x[i][j])) || (place.equals(x[i][j]))) { 
        int row = i; 
        int column = j; 

        printArray(x, i, j); 

       } 

      } 
     } 
    } 


    public static void printArray(String x[][], int product, int place) { 
     for (int row = 0; row < 1; row++) { 
      for (int column = 0; column < 1; column++) 

       System.out.print(x[product][place] + "\t"); 

     } 
    } 


} 

當我運行該程序輸出我輸入的單詞,而不是數量。我使用搜索方法,它確實給出了正確的數字,但是當它打印時,它不會打印正確的答案,對於我能做些什麼來解決問題有幫助?

回答

1

您可能想要在兩種方法中改進for循環。 嘗試下面的代碼,

import java.util.Scanner; 


public class Searching 
{ 


public static void main(String[] args) 
{ 
String[][] firstarray = 
{{"", "Store101", "Store102", "Store103", "Store104"}, 
{"Tennis Shoes", "102", "54", "20", "78"}, 
{"Sweaters", "45", "25", "35", "75"}, 
{"Jeans", "12", "35", "45", "65"}, 
{"Shorts", "54", "25", "34", "45"}, 
{"Jackets", "15", "35", "50", "25"} 
}; 

Scanner input = new Scanner(System.in); 
System.out.println("Enter item"); 
String userintake = input.next(); 
System.out.println("Enter Store Number"); 
String store = input.next(); 

searchingitem(firstarray, userintake, store); 

} 

private static void searchingitem(String[][] x, String item, String place) 
    { 
    int row =0 , col = 0 ; 
    boolean itemFound = false , storeFound = false ; 

    //Check if item is present and its row number. 
    for(int i = 0 ; i < x.length ; i++) 
    { 
     if(x[i][0].equalsIgnoreCase(item)) 
     { 
      row = i ; 
      itemFound = true ; 
      break; 
     } 
    } 

    //Check if item is present in the array or return form the method by giving an error. 
    if(!itemFound) 
    { 
     System.out.println("Item not found.\n"); 
     return; 
    } 

    //Getting store 
    for(int j = 0 ; j < x[0].length ; j++) 
    { 
     if(x[0][j].equalsIgnoreCase(place)) 
     { 
      col = j ; 
      storeFound = true ; 
      break; 
     } 
    } 

    if(!storeFound) 
    { 
     System.out.println("Can not find store number.\n"); 
     return; 
    } 

    //You do not need this method. you can directly printout from here but since you have made this method, I will use it. 
    printArray(x, row, col); 
    } 



public static void printArray(String x[][], int product, int place) 
{ 
    //Primting out the no of items available in store. 
    System.out.println(" Item: "+ x[product][0] + "\n Store : " + x[0][place] + "\n Availalbe units: " + x[product][place]); 
} 


} 

我已經作出以下改動。

1 - 而不是通過所有值檢查,我已經做了兩個對於爲店號項目和第一行只檢查第一列循環。

2 - 如果項目或商店沒有發現它提供了錯誤並返回。

3 - 而不是在打印方法循環中運行,我只是把這樣的println它打印必要的東西。

+0

@ user3373392:請標記爲答案,如果它做了這項工作。 –