Cannot find symbol while loop

2019-08-10 14:21发布

问题:

Hello I am creating an algorithm to take int x and convert it to the desired base being int y. example 7 base 3 = 21.

void printXBaseY(int x, int y) {

  boolean active = true; 

  while(x >= y) {
      int a = x % y;
      int b = x / y;


      String first = "" + a;
      String second = "" + b;

      String answer = "" + first + second;

  }

  String answer = "" + first + second;

  println(x + " base " + y + " is " + answer);

}

}

at String answer it has error cannot find symbol - variable first, can anyone explain why it cannot find it? and provide a solution.

thank you in advance

回答1:

Those variable's are out of scope.

In java the scope is restricted to {}.

Just move them to top, so that they're available further.

void printXBaseY(int x, int y) {

          boolean active = true; 
          String first=""; //  or null
          String second=""; // or null 
          while(x >= y) {
              int a = x % y;
              int b = x / y;


               first = "" + a;
               second = "" + b;

              String answer = "" + first + second;

          }

          String answer = "" + first + second;

          System.out.println(x + " base " + y + " is " + answer);

        }

You might be a beginner :Read more about block and statements



回答2:

It is out of scope. You declared it within the while loop. it is gone afterwards.

To solve this, declare first and second before the while loop starts.



回答3:

The scope of variable "first" is bounded by while block. So it cannot be accessed outside it.



回答4:

Your first and second variables are declared inside while loop. So the scope of them are inside while loop only you can not use them outside while loop.

void printXBaseY(int x, int y) {

  boolean active = true; 
  String first = null;
  String second = null
  while(x >= y) {
      int a = x % y;
      int b = x / y;


      first = "" + a;
      second = "" + b;

      String answer = "" + first + second;

  }

  String answer = "" + first + second;

  println(x + " base " + y + " is " + answer);

}


回答5:

 while(x >= y) {
     int a = x % y;
     int b = x / y;


     String first = "" + a;  // here is the problem. You declared first and second within the while loop.
     String second = "" + b;

     String answer = "" + first + second;

 } 

Corrected code below

while(x >= y) {
      int a = x % y;
      int b = x / y;


      String first = "" + a;
      String second = "" + b;

      String answer = "" + first + second;



  String answer = "" + first + second;

  println(x + " base " + y + " is " + answer);

 }


回答6:

Your variables first and second as well are declared inside your while loop, therefore its lifetime is bound within that loop. If it is not clear for you what a scope is, you should read this interesting slide http://classes.soe.ucsc.edu/cmps012a/Winter03-01/notes/Lecture27-4perpage.pdf



回答7:

void printXBaseY(int x, int y) {

  boolean active = true;   
 String first="";    
String second="";   
  String answer="";     

  while(x >= y) {  
      int a = x % y;  
      int b = x / y;  


       first = "" + a;
      second = "" + b;

   //  answer = "" + first + second;

  }

  answer = "" + first + second;

  println(x + " base " + y + " is " + answer);

}