循环时找不到符号 [英] Cannot find symbol while loop

查看:135
本文介绍了循环时找不到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在创建一种算法,将int x转换为所需的基数int y. 示例7以3为基数=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.

先谢谢您

推荐答案

这些变量不在范围内.

在Java中,范围限制为{}.

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

这篇关于循环时找不到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆