将用户输入保存到数组中并确保它不违反索引

时间:2011-07-04 00:16:17

标签: java arrays

我正在进行作业并编辑程序。我要求用户输入他们的销售员编号,产品编号以及销售量。我试图将销售数据保存为名为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 个答案:

答案 0 :(得分:3)

好吧,我的Java非常生疏(我只是开车去编辑)但是我甚至可以看到:

您在此处使用符号sales

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

...但是你在这里使用完全相同的符号:

double sales = input.nextInt();

......这在任何语言中都是不好的做法。即使不是VM,它也会让人感到困惑。

我怀疑你对这一行有疑问:

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

......就是说,即使VM确定了你想要的两个sales中的哪一个,你也从未将符号amount定义为任何意义。我想你想要的实际上是:

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

如果你反复查看相同的代码,很容易错过这种事情。您开始看到您打算键入的内容以及关联的逻辑,而不是代码实际读取的内容。每个人都这样做,即使是老手。

答案 1 :(得分:0)

如果您担心违反索引,可以尝试使用Map存储日期。如:

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.