Cannot find symbol while loop

◇◆丶佛笑我妖孽 提交于 2020-01-15 15:24:13

问题


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);

}


来源:https://stackoverflow.com/questions/19044573/cannot-find-symbol-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!