重复的局部变量和变量无法解析

时间:2014-05-01 21:11:49

标签: java

我的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个销售单。

     

假设上个月所有单据中的信息都可用。编写一份应用程序,读取上个月销售的所有信息,并按销售人员和产品汇总销售总额。所有总计应存储在二维数组销售中。处理完一个月的所有信息(数据文件中的所有记录)后,以表格格式显示结果,每列代表特定产品,每行代表特定销售人员。每行交叉总计以获得该月每种产品的总销售额。对每一列进行交叉总计,以获得该销售人员当月的总销售额。表格输出应包括总计行右侧和总计列底部的这些总计。

3 个答案:

答案 0 :(得分:3)

您声明了产品两次,当它们超出范围时,您会引用productTotal和salesPersonTotal。尝试更改第二个产品变量的名称(如果您的解决方案没有问题,请使用相同的变量),并尝试声明productTotal和salesPersonTotal一个范围阻止。

答案 1 :(得分:1)

使变量成为全局变量,您只能声明变量一次:

import java.util.Random;
import java.util.Scanner; 
class TotalSales
{
    public static void main( String args[] )
    {
        Scanner input = new Scanner( System.in );

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

        double productTotal = 0.0;
        double salesPersonTotal [] = new double[ 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: " );
                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();
            }



            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++ )
            {
                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();
    }
}

答案 2 :(得分:0)

在循环内创建的任何变量都是循环的局部变量。这意味着一旦退出循环,就无法再访问该变量。如果要在离开循环后使用变量,则必须事先在外部循环或方法范围内声明它。