1

This is the error I am getting and I cant seem to figure out how to fix it. I was trying to make it so that once you have finished your calculations, the program asks you if you want to perform them again.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: retry cannot be resolved yes cannot be resolved to a variable

My code(don't judge, this is my first program in java):

package calculatorpls;

import java.util.Scanner;

public class calc {

    public static void main(String[]Args)


    {

        do {

    Scanner num = new Scanner(System.in);

    System.out.println("Please enter the first number."+"\n");

    int no1 = num.nextInt();

        System.out.println("\n"+"The number you have entered is "+no1+"."+"\n"+"\n"+"Please enter the second number now."+"\n");

    int no2 = num.nextInt();

        System.out.println("\n"+"The number you have entered is "+no2+"."+"\n"+"\n"+"Please choose what you would like to do from the following options:"+"\n"+
        "1)Addition"+"\n"+"2}Subtraction(1st Number-2nd Number)"+"\n"+"3)Subtraction(2nd Number-1st Number)"+"\n"+"4)Multiplication"+"\n"+"5)Division(1st Number divided by 2nd Number)"+"\n"+"6)Division(2nd Number divided by 1st Number)"
                + ""+"\n"+"7)Multiply by an exponent(1st Number)"+"\n"+"8)Multiply by an exponent(2nd Number)"+"\n"+"\n"+"Type any number from 1-8 to select your option."+"\n");

    //String Addition;
    //String Subtraction(1st Number-2nd Number);
    //String Subtraction(2nd Number-1st Number);    
    //String Multiplication;
    //String Division(1st Number divided by 2nd Number);
    //String Division(2nd Number divided by 1st Number);
    //String Multiply by an exponent(1st Number);
    //String Multiply by an exponent(2nd Number);

        int choice = num.nextInt();

                System.out.println("\n"+"You have chosen "+choice +"\n");


        switch (choice)
        {       
        case 1: 

            float addition = no1+no2;
            System.out.println("\n"+ addition);
            break; 

        case 2: 

            float subtraction1 = no1-no2;
            System.out.println("\n"+ subtraction1);
            break; 

        case 3: 

            float subtraction2 = no2-no1;
            System.out.println("\n"+ subtraction2);
            break; 

        case 4: 

            float multiplication = no1*no2;
            System.out.println("\n"+ multiplication);
            break;

        case 5: 

            double division1 = no1/no2;
            System.out.println("\n"+ division1);
            break;

        case 6: 

            double division2 = no2/no1;
            System.out.println("\n"+ division2);
            break; 

        case 7: 

        System.out.println("\n"+ "Please enter the power."+"\n");
            int exponent = num.nextInt();
            double exponent1 = (int) Math.pow(no1, exponent);
            System.out.println("\n"+ exponent1);
            break;

       case 8: 

           System.out.println("\n"+ "Please enter the power."+"\n");
            int exponenttwo = num.nextInt();
            double exponent2 = (int) Math.pow(no2, exponenttwo);
            System.out.println("\n"+ exponent2);
            break;

        default: 

            System.out.println("\n"+ "There isnt any such option matching your entry!"+"\n");


            break; 

        }

        System.out.println("\n"+ "Would you like to perform more calculations? Respond with yes or no."+"\n");

        String retry = num.nextLine();
         String again = "yes";

    }while(retry.equalsIgnoreCase(again));

    }

}
Luca Murra
  • 1,848
  • 14
  • 24
Vishesh
  • 11
  • 2

2 Answers2

0

I fixed it for you.

         String retry;
         String again;
     do {

         Scanner num = new Scanner(System.in);

         System.out.println("Please enter the first number."+"\n");

         int no1 = num.nextInt();

             System.out.println("\n"+"The number you have entered is "+no1+"."+"\n"+"\n"+"Please enter the second number now."+"\n");

         int no2 = num.nextInt();

             System.out.println("\n"+"The number you have entered is "+no2+"."+"\n"+"\n"+"Please choose what you would like to do from the following options:"+"\n"+
             "1)Addition"+"\n"+"2}Subtraction(1st Number-2nd Number)"+"\n"+"3)Subtraction(2nd Number-1st Number)"+"\n"+"4)Multiplication"+"\n"+"5)Division(1st Number divided by 2nd Number)"+"\n"+"6)Division(2nd Number divided by 1st Number)"
                     + ""+"\n"+"7)Multiply by an exponent(1st Number)"+"\n"+"8)Multiply by an exponent(2nd Number)"+"\n"+"\n"+"Type any number from 1-8 to select your option."+"\n");

         //String Addition;
         //String Subtraction(1st Number-2nd Number);
         //String Subtraction(2nd Number-1st Number);    
         //String Multiplication;
         //String Division(1st Number divided by 2nd Number);
         //String Division(2nd Number divided by 1st Number);
         //String Multiply by an exponent(1st Number);
         //String Multiply by an exponent(2nd Number);

             int choice = num.nextInt();

                     System.out.println("\n"+"You have chosen "+choice +"\n");


             switch (choice)
             {       
             case 1: 

                 float addition = no1+no2;
                 System.out.println("\n"+ addition);
                 break; 

             case 2: 

                 float subtraction1 = no1-no2;
                 System.out.println("\n"+ subtraction1);
                 break; 

             case 3: 

                 float subtraction2 = no2-no1;
                 System.out.println("\n"+ subtraction2);
                 break; 

             case 4: 

                 float multiplication = no1*no2;
                 System.out.println("\n"+ multiplication);
                 break;

             case 5: 

                 double division1 = no1/no2;
                 System.out.println("\n"+ division1);
                 break;

             case 6: 

                 double division2 = no2/no1;
                 System.out.println("\n"+ division2);
                 break; 

             case 7: 

             System.out.println("\n"+ "Please enter the power."+"\n");
                 int exponent = num.nextInt();
                 double exponent1 = (int) Math.pow(no1, exponent);
                 System.out.println("\n"+ exponent1);
                 break;

            case 8: 

                System.out.println("\n"+ "Please enter the power."+"\n");
                 int exponenttwo = num.nextInt();
                 double exponent2 = (int) Math.pow(no2, exponenttwo);
                 System.out.println("\n"+ exponent2);
                 break;

             default: 

                 System.out.println("\n"+ "There isnt any such option matching your entry!"+"\n");


                 break; 

             }

             System.out.println("\n"+ "Would you like to perform more calculations? Respond with yes or no."+"\n");

             retry = num.next();
             again = "yes";

         }while(retry.equalsIgnoreCase(again));
nusaK
  • 1
  • 2
0

I'm answering just to expand on nusaK's answer.

  1. You should declare utilities like Scanner which may be used multiple times outside of for loops.

When you put the Scanner num = new Scanner(System.in); inside a loop, a new Scanner Object will be created every time the loop runs. Since we're always scanning from System.in, we can use the same object to scan for all iterations.

Since Java manages memory by itself, it isn't much of a problem but it might be in other languages.

Scanner num = new Scanner(System.in);

String again = "yes"; // you can initialize again here 
String retry=""; // always initialize in languages like Java to avoid errors.

do {
   ...
}
while((retry=num.next()).equalsIgnoreCase(again));
// u can also use this, i.e. assign and evaluate at the same time but it's harder to read.
Jeffrey
  • 75
  • 9