2011-07-04 83 views
0

我正在進行作業和編輯程序。我要求用戶輸入他們的銷售人員編號,產品編號和銷售量。我試圖將銷售數據保存爲一個名爲sales的數組。但是,我無法正確訪問二維數組的元素。將用戶輸入保存到數組中並確保它不違反索引

數組定義爲:

double[][] sales = new double[ 5 ][ 4 ] 

但是當我嘗試這樣做:

sales[ product - 1 ][ person - 1 ] += amount; 

...它不保存增加銷售額。我認爲我違反了陣列的索引。

這裏是整個代碼塊:

import java.util.Scanner; 

public class Sales2 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     // sales array holds data on number of each product sold 
     // by each salesperson 
     double[][] sales = new double[ 5 ][ 4 ]; // 5 salespeople, 
               //4 products each person 

     System.out.print("Enter salesperson number (-1 to end): "); 
     int person = input.nextInt(); // the salesperson index 

     while (person != -1) 
     { 
     System.out.print("Enter product number: "); 
     int product = input.nextInt(); // the product index 
     // prompt user to enter product number and save it as an integer 
     System.out.print("Enter sales amount: "); 
     double sales = input.nextInt(); 
    // promp to enter sales amont and save it as double 

     sales[ product - 1 ][ person - 1 ] += amount; 
     // Having trouble with the following. I tried to manipulate 
       // the above array but nothing will work. thanks 
       // error-check the input number for the array boundary 
     // that is the person index should be 0 - 3 
     // and the product index should be 0 - 4 
     // notice that array index start with 0 
     // save the input to the sales 
       //array like sales[ product - 1 ][ person - 1 ] += amount; 
     // or print message for the out of boundary input 


     System.out.print("Enter salesperson number (-1 to end): "); 
     person = input.nextInt(); // input for next sales person 
     } // end while 


     // total for each salesperson 
     double[] salesPersonTotal = new double[ 4 ]; 

     // display the table  
     for (int column = 0; column < 4; column++) 
     salesPersonTotal[ column ] = 0; // Initialize the array 

     System.out.printf("%8s%14s%14s%14s%14s%10s\n", 
      "Product", "Salesperson 1", "Salesperson 2", 
      "Salesperson 3", "Salesperson 4", "Total"); 

     // To do - 
     // for each column of each row, print the appropriate 
     // value representing a person's sales of a product 
     // and calculate and print out the total for each product 


     System.out.printf("%25s","1", "2", "3", 
      "4", "5"); 

     // To do - 
     // print out for each sales person total 
      // I have been messing with these numbers but 
      //it doesnt seem to be working. 
    } // end main 
} // end class Sales2 
+2

您可能想將其作爲一個實際問題。 – weltraumpirat

+0

@weltraumpirat你是什麼意思? –

+0

你從來沒有問過。只是發佈你的任務,並等待別人解決它不會對你或任何其他人有任何好處。如果您在理解數組時遇到問題,那麼詢問如何訪問二維數組中的值,或者如果您想對用戶輸入進行值檢查,請提問 - 無論哪種方式,使這個實際問題將幫助其他人解決您的問題具體問題,並且它還將使其他用戶能夠找到他們自己問題的答案,如果它是相關的。 – weltraumpirat

回答

3

好了,我的Java是極其生鏽(我只是開車經過編輯),但即使我可以看到:

您使用符號sales這裏:

double[][] sales = new double[ 5 ][ 4 ]; 

...但你在這裏使用完全相同的符號:

double sales = input.nextInt(); 

......這是在任何語言的不好的做法。即使不是虛擬機,它也會混淆人類。

我懷疑你的問題與該行:

sales[ product - 1 ][ person - 1 ] += amount; 

...的是,即使虛擬機確實弄清楚你打算這兩個sales的,好了,你沒有定義符號amount意思任何東西。我想你想要的是實際:

double amount = input.nextInt(); 
    sales[ product - 1 ][ person - 1 ] += amount; 

這種事情很容易,如果你在相同的代碼一遍又一遍地看錯過。你開始看到你想要輸入的內容以及相關的邏輯,而不是代碼實際讀取的內容。每個人都這樣做,甚至是老手。

0

如果你擔心違反指數,你可以嘗試用地圖來存儲您的日期。如:

Map<Integer,Map<Integer,Integer>> saleMap= new HashMap<Integer,Map<Integer,Integer>>; 
Map<Integer,Integer> productMountMap = new HashMap<Integer,Integer>; 
productMountMap.put(product,mount); 
//.. add more proudct mount map as you like. you don't need worry about the violate index 
saleMap.put(person,productMountMap); 

//Get one person's data using "person" as the key. 
Map<Integer,Integer> b = saleMap.get(person) // so you get the product mount map. 
+0

謝謝.....我認爲它可以不擔心在這種情況下的揮發性指數。有沒有辦法只是定義一個新的數組來處理它。我試圖用銷售[product - 1] [person - 1] + = amount來做這件事。但那不是正確的 –

+0

所以你可以試試地圖數據結構。地圖的內部工具也是Array。由於數組必須是靜態大小,或者每次插入值時檢查索引,如果違反,只需新建一個新數組,則複製舊數據,插入新數據。這是列表的方式。 – x4snowman

+0

好吧,我用上面的代碼創建了映射和哈希映射的導入,但是正在編譯錯誤。 –

相關問題