-2
import java.util.Scanner;

class calculator {
    void add(float a, float b)
    {
        System.out.print("Result: "+(a+b));
    }
    void sub(float a, float b)
    {
        System.out.print("Result: "+(a-b));
    }
    void mult(float a, float b)
    {
        System.out.print("Result: "+(a*b));
    }
    void div(float a, float b)
    {
        System.out.print("Result: "+(a/b));
    }
}

class Cal {
    public static void main(String args[]) {
        Scanner sc= new Scanner(System.in);

        do {
            System.out.print("Enter Two Operands: ");
            float a=sc.nextFloat();

            float b=sc.nextFloat();

            System.out.print("Press 1 for Addition\n Press 2 for
            Subtraction\n Press 3 for Multiplication\n Press 4 for 
            Division\n Press 5 for Exit\n");

            System.out.print("Enter your choice: ");
            int n=sc.nextInt();
            calculator c=new calculator();
            switch(n)
            {
                case 1: c.add(a,b);
                        break;
                case 2: c.sub(a,b);
                        break;
                case 3: c.mult(a,b);
                        break;
                case 4: c.div(a,b);
                        break;
                case 5: System.exit(0);
                default: System.out.print("Wrong Choice!!!");
            }
            System.out.print("Are you want to continue?: ");
            int con=sc.nextInt();
        } while (con==1);
    }
}

It is simple calculator problem but I face below given error:

PS H:\java> javac calculator.java

calculator.java:47: error: cannot find symbol
                }while(con==1);
                       ^
  symbol:   variable con
  location: class Cal
1 error
khelwood
  • 55,782
  • 14
  • 81
  • 108
DigiLeon
  • 76
  • 1
  • 2
  • 10

1 Answers1

0

The con variable is declared inside the loop and not visible to while.

Declare int con=0; before do-while loop starts. and remove int from con value assignment in the loop.

fiveelements
  • 3,649
  • 1
  • 17
  • 16