【问题标题】:Cannot find symbol while loop循环时找不到符号
【发布时间】:2013-10-03 09:15:30
【问题描述】:

您好,我正在创建一个算法来获取 int x 并将其转换为所需的基数,即 int y。 示例 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);

}

}

在字符串答案中出现错误找不到符号 - 首先是变量,谁能解释为什么找不到它?并提供解决方案。

提前谢谢你

【问题讨论】:

    标签: java variables symbols base


    【解决方案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);
    
            }
    

    您可能是初学者:阅读更多关于block and statements

    【讨论】:

      【解决方案2】:

      超出范围。您在 while 循环中声明了它。后来就没了。

      要解决这个问题,请在 while 循环开始之前声明 first 和 second。

      【讨论】:

        【解决方案3】:

        变量“first”的范围由while 块限定。所以它不能在它之外访问。

        【讨论】:

          【解决方案4】:

          您的第一个和第二个变量在 while 循环中声明。所以它们的作用域只在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);
          
          }
          

          【讨论】:

            【解决方案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);
            
             }
            

            【讨论】:

            • 我在发帖前试过了,但循环永远不会结束,谢谢
            【解决方案6】:

            您的变量 firstsecond 也在您的 while 循环中声明,因此它的生命周期被绑定在该循环中。 如果您不清楚范围是什么,您应该阅读这张有趣的幻灯片 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);
              
              }
              

              【讨论】:

                猜你喜欢
                • 2023-03-15
                • 2014-04-14
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-04-11
                相关资源
                最近更新 更多