Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将用户输入保存到数组中并确保它不违反索引_Java_Arrays - Fatal编程技术网

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

Java 将用户输入保存到数组中并确保它不违反索引,java,arrays,Java,Arrays,我正在做作业和编辑程序。我要求用户输入他们的销售人员编号、产品编号和销售金额。我正在尝试将销售数据保存为名为sales的数组。但是,我无法正确访问二维数组的元素 该数组定义为: double[][] sales = new double[ 5 ][ 4 ] 但当我尝试这样做时: sales[ product - 1 ][ person - 1 ] += amount; 。。。它不会保存销售金额的增量。我想我违反了数组的索引 以下是整个代码块: import java.util.Scanne

我正在做作业和编辑程序。我要求用户输入他们的销售人员编号、产品编号和销售金额。我正在尝试将销售数据保存为名为
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
import java.util.Scanner;
公务舱销售2
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
//sales数组保存有关每个已销售产品的数量的数据
//每个销售人员
double[]sales=新的double[5][4];//5名销售人员,
//每人4件产品
系统输出打印(“输入销售人员编号(-1到结尾):”;
int person=input.nextInt();//销售人员索引
而(人!=-1)
{
系统输出打印(“输入产品编号:”);
int product=input.nextInt();//产品索引
//提示用户输入产品编号并将其保存为整数
系统输出打印(“输入销售金额:”);
double sales=input.nextInt();
//提示输入sales amont并将其保存为double
销售额[产品-1][人-1]+=金额;
//有以下问题。我试图操纵
//上面的数组无效。谢谢
//检查数组边界的输入编号时出错
//即人员索引应为0-3
//产品指数应为0-4
//请注意,数组索引以0开头
//将输入保存到sales
//类似数组的销售额[产品-1][人-1]+=金额;
//或打印边界外输入的消息
系统输出打印(“输入销售人员编号(-1到结尾):”;
person=input.nextInt();//下一个销售人员的输入
}//结束时
//每个销售人员的总数
double[]salesPersonTotal=新的double[4];
//显示表格
for(int列=0;列<4;列++)
salersOnTotal[column]=0;//初始化数组
System.out.printf(“%8s%14s%14s%14s%14s%14s%10s\n”,
“产品”、“销售人员1”、“销售人员2”,
“销售人员3”、“销售人员4”、“总计”);
//做-
//对于每行的每一列,打印相应的
//代表一个人对一种产品的销售的价值
//并计算并打印出每种产品的总数
System.out.printf(“%25s”、“1”、“2”、“3”,
"4", "5" );
//做-
//打印每个销售人员的总计
//我一直在搞乱这些数字,但是
//它似乎不起作用。
}//末端总管
}//终端类销售2

如果您担心违反索引,您可以尝试使用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.
Map saleMap=newhashmap;
Map productMountMap=新的HashMap;
productMountMap.put(产品,挂载);
//.. 添加更多您喜欢的地图。您不需要担心索引的冲突
saleMap.put(person,productMountMap);
//使用“person”作为键获取一个人的数据。
Map b=saleMap.get(person)//这样就得到了产品挂载图。

如果您担心违反索引,您可以尝试使用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.
Map saleMap=newhashmap;
Map productMountMap=新的HashMap;
productMountMap.put(产品,挂载);
//.. 添加更多您喜欢的地图。您不需要担心索引的冲突
saleMap.put(person,productMountMap);
//使用“person”作为键获取一个人的数据。
Map b=saleMap.get(person)//这样就得到了产品挂载图。

好吧,我的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;

如果您反复查看同一代码,很容易忽略这类内容。您开始看到您想要键入的内容以及相关的逻辑,而不是代码实际读取的内容。每个人都这样做,即使是老手。

好吧,我的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;

如果您反复查看同一代码,很容易忽略这类内容。您开始看到您想要键入的内容以及相关的逻辑,而不是代码实际读取的内容。每个人都这样做,即使是老手。

你可能想把这个问题变成一个实际问题。@weltraumpirat你什么意思?你从来没问过。仅仅发布你的作业并等待其他人解决它对你或其他任何人都没有任何好处。如果您在理解数组时遇到问题,请询问如何访问二维数组中的值,或者如果您想检查用户输入的值,请询问该问题-无论是哪种方式,都要让这成为一个实际问题