找不到while循环符号(Cannot find symbol while loop)

2019-10-18 16:43发布

你好我创建的算法采取INT X并将其转换为所需的基本是INT年。 实施例7基体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);

}

}

在字符串回答它的主要错误无法找到符号 - 可变第一,任何人都可以解释为什么它不能找到它? 并提供一种解决方案。

先感谢您

Answer 1:

这些变量超出范围。

在Java中的范围被限制为{}

只需将它们移动到顶部,这样他们可以进一步。

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

        }

你可能是一个初学者:了解更多关于块和语句



Answer 2:

这是超出范围。 您在while循环内声明它。 这是继去。

为了解决这个问题,声明while循环开始前的第一和第二。



Answer 3:

可变的“第一”的范围的同时,通过块边界。 因此,它不能外面访问。



Answer 4:

您的第一个和第二个变量while循环内部声明。 所以,他们的范围都在里面,而仅环不能外面使用它们while循环。

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

}


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;

 } 

下面的代码更正

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

 }


Answer 6:

您的变量firstsecond ,以及被宣布while循环内,因此它的寿命是循环中的约束。 如果你不明确的一个范围是什么,你应该读了这个有趣的幻灯片http://classes.soe.ucsc.edu/cmps012a/Winter03-01/notes/Lecture27-4perpage.pdf



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

}


文章来源: Cannot find symbol while loop