duplicate local variable and variables cannot be resolved

后端 未结 3 1761
庸人自扰
庸人自扰 2021-01-27 02:53

I\'m being given three different errors in my java code, any help?

Error one: Duplicate local variable product.

int product = input.nextInt();
相关标签:
3条回答
  • 2021-01-27 03:08

    You declared product twice, and you refer to productTotal and salesPersonTotal when they are out of scope. Try changing the second product variable's name (or use the same variable if that's okay with your solution), and try declaring productTotal and salesPersonTotal one scope block up.

    0 讨论(0)
  • 2021-01-27 03:22

    Any variables created inside of a loop are local to the loop. This means that once you exit the loop, the variable can no longer be accessed. If you want to use the variable after you left the loop, then you have to declare it before beforehand in the outer loop or method scope.

    0 讨论(0)
  • 2021-01-27 03:25

    Make variables global and you can declare a variable only once:

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题