-2

First, this is a homework assignment. I am supposed to create a program using a switch command. It asks the user to input 3 integers, then input an integer of 1-5 for the five cases average, max, min, total, and exit. I have pretty much completed this program, first having a user input 3 integers, then creating the switch "menu" structure, and inside each case, I attempted to create the mathematics for each of the arithmetic functions.

My error message is "cannot find symbol", which is a broad and vague error message to post about. Everything I have Googled, or found on this site seems to be unrelated to the specific symbols that my program is missing, and/or can be typographical in nature. So, unfortunately, I've had to create another thread about this.

My program can not find the symbols average, max, min, and et cetera. I believe that I may have done my arithmetic algorithms wrong, and they are either not initialized properly or at all, or I need to set them to zero before the switch command is ever used.

I'll need to post the code so you can see what I have done:

>public static void main( String args[] )
  {
    // Attempting to initialize the 3 integers AND the user-input early on
    int n1, n2, n3, n4; 

    Scanner sc = new Scanner( System.in);

    while ( true )
    {
        System.out.println ( "Please give three integers separated by spaces: " );
        n1 = sc.nextInt();
        n2 = sc.nextInt();
        n3 = sc.nextInt();

        // building a menu - hoping integers aboive will work in Switch command
        System.out.println( "\n****** Analysis Menu ******" );
        System.out.println( "1: Average" );
        System.out.println( "2: Maximum" );
        System.out.println( "3: Minimum" );
        System.out.println( "4: Total" );
        System.out.println( "5: Exit" );
        System.out.println( "*******************\n" );

        n4 = sc.nextInt(); // get the user selection

        if ( n4 == 1 )
            {
                Average = n1 + n2 + n3 / 3;
                System.out.print( "Average = " + Average );
            }
        else if ( n4 == 2 )
            {
                max = n1;
                if ( max < n2 ) max = n2;
                if ( max < n3 ) max = n3;

                System.out.println( "Maximum = " + max );
            }
        else if ( n4 == 3 )
            {
                min = n1;
                if ( min > n2) min = n3;
                if ( min > n3) min = n2;
                System.out.print( "Minimum = " + min );
            }
        else if ( n4 == 4 )
            {
                total = sum;
                System.out.print( "total = " + total );
            }
        else if ( n4 == 5 )
            {
                System.out.println( "You have selected Exit." );
                break;
            }
        else
            {
                System.out.println( "No such selection exists." );

The error messages say they cannot find every total, min, max, average, and sum in this. So I am not sure if I need to initialize them somehow, set them to 0, or even how to do that in this context.

  • Why do you believe they may be "not initialized properly or at all"? – Jongware Oct 18 '14 at 13:44
  • you were asked to use switch statement, right ? but you are not using it at all...you are using if/else instead. – Sufiyan Ghori Oct 18 '14 at 13:47
  • I was not asked to explicitly use a switch statement, but what i was asked to create looked similar to a sample that I based this off of. And the "switch" statement that it used, used if/else statements for some reason, and I couldn't quite grasp the Case statement alternatives to try an actual switch instead. I also believed that maybe my average statement and such were not being found because I had not set them to zero or announced them, so the program maybe did not expect them or something. – 7vincent7black7 Oct 18 '14 at 13:51

2 Answers2

1

There are multiple issues in above code: 1. Average, min, max variable not defined. 2. You average logic is not right. 3. You are missing end brace.

I tried to modify these and this is what i came up with.

public static void main( String args[] )
{
// Attempting to initialize the 3 integers AND the user-input early on
int n1, n2, n3, n4; 
int Average, max, min, total;
Scanner sc = new Scanner( System.in);

while ( true )
{
    System.out.println ( "Please give three integers separated by spaces: " );
    n1 = sc.nextInt();
    n2 = sc.nextInt();
    n3 = sc.nextInt();

    // building a menu - hoping integers aboive will work in Switch command
    System.out.println( "\n****** Analysis Menu ******" );
    System.out.println( "1: Average" );
    System.out.println( "2: Maximum" );
    System.out.println( "3: Minimum" );
    System.out.println( "4: Total" );
    System.out.println( "5: Exit" );
    System.out.println( "*******************\n" );

    n4 = sc.nextInt(); // get the user selection

    if ( n4 == 1 )
        {
            Average = (n1 + n2 + n3) / 3;
            System.out.print( "Average = " + Average );
        }
    else if ( n4 == 2 )
        {
            max = n1;
            if ( max < n2 ) max = n2;
            if ( max < n3 ) max = n3;

            System.out.println( "Maximum = " + max );
        }
    else if ( n4 == 3 )
        {
            min = n1;
            if ( min > n2) min = n3;
            if ( min > n3) min = n2;
            System.out.print( "Minimum = " + min );
        }
    else if ( n4 == 4 )
        {
            total = n1 + n2 + n3;
            System.out.print( "total = " + total );
        }
    else if ( n4 == 5 )
        {
            System.out.println( "You have selected Exit." );
            break;
        }
    else
        {
            System.out.println( "No such selection exists." );
        }
}
}
SMA
  • 36,381
  • 8
  • 49
  • 73
  • 1. You are absolutely right, thank you. 2. Thank you for that as well. I didn't even think about how the 3 numbers would not be added together independent of the division. 3. Rest assured. :) I wanted to be able to trim the amount of code I put in my question, but could not because my if/else statement was so long and extensive. One of the things I did do, was leave out my end braces because they could have been seen as unrelated. 4. Here's another I just now fixed. Some of my Else statements use "print" instead of "println". So the followup userinput, gets tacked onto the output. – 7vincent7black7 Oct 18 '14 at 14:35
0

You need to define Average,min,max,total as a variable just like n1,..,n4. I mean decimal Average;

BTW it's better to avoid naming variable like this for example minValue or maxValue are better in naming.

Reza
  • 18,865
  • 13
  • 88
  • 163