我的 java 代码中出现了三个不同的错误,有什么帮助吗?
错误一:重复的局部变量乘积。
int product = input.nextInt();
错误 2:productTotal 无法解析为变量。
System.out.printf( "%10.2f\n", productTotal );
错误 3:salesPersonTotal 无法解析为变量。
System.out.printf( "%14.2f", salesPersonTotal[ column ]);
Java代码:
import java.util.Scanner;
class TotalSales
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
double sales[][] = new double [ 5 ] [ 4 ];
System.out.print( "Enter salesperson number (-1 to end): " );
int person = input.nextInt();
while ( person != -1 )
{
System.out.print( "Enter product number: ");
int product = input.nextInt();
while ( person != -1 )
{
System.out.print( "Enter product number: " );
int product = input.nextInt();
System.out.print( "Enter sales amount: ");
double amount = input.nextDouble();
if ( person >= 1 && person <5 &&
product >= 1 && product <6 && amount >=0 )
sales[ product - 1 ] [ person -1 ] += amount;
else
System.out.println("Invalid input!" );
System.out.print( "Enter slaesperson number (-1 to end): " );
person = input.nextInt();
}
double salesPersonTotal [] = new double[ 4 ];
for (int column = 0; column < 4; column++ )
salesPersonTotal[ column ] = 0;
System.out.printf( "%8s%14s%14s%14s%14s%10s\n",
"Product", "Salesperson 1", "Salesperson 2",
"Salesperson 3", "Salesperson 4", "Total" );
for ( int row = 0; row < 5; row++ )
{
double productTotal = 0.0;
System.out.printf( "%8d", (row+1));
for ( int column = 0; column < 4; column ++ ) {
System.out.printf( "%14.2f", sales[row] [column]);
productTotal += sales[row][column];
salesPersonTotal[column] += sales[row][column];
}
System.out.printf( "%10.2f\n", productTotal );
}
System.out.printf( "%10.2f\n", productTotal );
}
System.out.printf( "%8s", "Total" );
for (int column = 0; column < 4; column++ )
System.out.printf( "%14.2f", salesPersonTotal[ column ]);
System.out.println();
}
}
附加信息:
问题:(TotalSales)使用二维数组解决以下问题:一家公司有四名销售人员(1 到 4),他们销售五种不同的产品(1 到 5)。每天,每个销售人员都会为每种销售的产品传递一张单据。每张单据包含以下内容:
a) 营业员编号(1至4)
b) 产品编号
c) 当天销售的产品(1 至 5)的总美元价值。因此,每个销售人员每天传递 0 到 5 个销售单。
假设上个月所有单据的信息均可用。编写一个应用程序,该应用程序将读取上个月销售的所有这些信息,并按销售人员和产品汇总总销售额。所有总计应存储在二维数组 sales 中。处理完一个月的所有信息(数据文件中的所有记录)后,以表格形式显示结果,每一列代表一个特定的产品,每一行代表一个特定的销售人员。对每一行进行交叉求和即可得到当月每种产品的总销售额。对每列进行交叉求和即可得到该月销售人员的总销售额。您的表格输出应在总计行的右侧和总计列的底部包含这些交叉总计。