在不同类中的for循环后返回变量。

huangapple go评论93阅读模式
英文:

return variable after for loop in a different class

问题

以下是您要翻译的内容:

我尝试在不同的类中创建一个 for 循环,并希望在主函数外部获取循环的最后一个值。问题是:无法解析 x...
我还想在不同的类中有许多值和一个 for 循环。我该如何解决这个问题?任何帮助都非常受欢迎。提前感谢您。

  1. public class Javaapp {
  2. public static void main(String[] args) {
  3. Data a = new Data();
  4. int[] getData = a.returnData();
  5. a.for1();
  6. System.out.println(a.x); // 无法解析 x,或者 x 不是一个字段
  7. System.out.println(a.k);
  8. }
  9. }
  10. //-------------------------------------------------
  11. public class Data {
  12. int k = 1;
  13. public double for1() {
  14. int x = 2;
  15. for (int i = 0; i < 10; i++) {
  16. x = x * x;
  17. }
  18. return x;
  19. }
  20. }
英文:

So i tried to create a for loop in a different class and wanted to get the last value of the loop outside to the main function. Problem is: x cannot be resolved...
I also want to have many values and a for loop in a different class. How do i fix this issue? Any help is very welcome. I thank you in advance.

  1. public class Javaapp {
  2. public static void main(String[] args) {
  3. Data a = new Data();
  4. int[] getData = a.returnData();
  5. a.for1();
  6. System.out.println(a.x); //x cannot be resolved or is not a field
  7. System.out.println(a.k);
  8. }
  9. }
  10. //-------------------------------------------------
  11. public class Data {
  12. int k=1;
  13. public double for1() {
  14. int x = 2;
  15. for (int i = 0; i &lt; 10; i++) {
  16. x = x *x;
  17. }
  18. return x;
  19. }
  20. }

答案1

得分: 0

访问循环结束后的循环值,您需要设置类属性或捕获函数的返回值。

尝试这段代码:

  1. public class Javaapp {
  2. public static void main(String[] args) {
  3. Data a = new Data();
  4. int[] getData = a.returnData();
  5. a.for1(); // 也可以调用: int x = a.for1();
  6. System.out.println(a.x); //x无法解析或不是字段
  7. System.out.println(a.k);
  8. }
  9. }
  10. //-------------------------------------------------
  11. public class Data {
  12. public int k=1; // 可以在类外部访问
  13. public int x=0;
  14. public double for1() {
  15. int x = 2;
  16. for (int i = 0; i < 10; i++) {
  17. x = x * x;
  18. }
  19. this.x = x; // 设置类属性
  20. return x; // 返回值
  21. }
  22. }
英文:

To access the loop value after the loop, you need to set the class property or capture the return value from the function.

Try this code:

  1. public class Javaapp {
  2. public static void main(String[] args) {
  3. Data a = new Data();
  4. int[] getData = a.returnData();
  5. a.for1(); // can also call: int x = a.for1();
  6. System.out.println(a.x); //x cannot be resolved or is not a field
  7. System.out.println(a.k);
  8. }
  9. }
  10. //-------------------------------------------------
  11. public class Data {
  12. public int k=1; // can access outside class
  13. public int x=0;
  14. public double for1() {
  15. int x = 2;
  16. for (int i = 0; i &lt; 10; i++) {
  17. x = x *x;
  18. }
  19. this.x = x; // set class property
  20. return x; // return value
  21. }
  22. }

答案2

得分: 0

尝试一下。

  1. public class Javaapp {
  2. public static void main(String[] args) {
  3. Data a = new Data();
  4. int[] getData = a.returnData();
  5. // a.for1();
  6. System.out.println(a.for1());
  7. System.out.println(a.k);
  8. }
  9. }
英文:

Try this.

  1. public class Javaapp {
  2. public static void main(String[] args) {
  3. Data a = new Data();
  4. int[] getData = a.returnData();
  5. // a.for1();
  6. System.out.println(a.for1());
  7. System.out.println(a.k);
  8. }
  9. }

huangapple
  • 本文由 发表于 2020年9月13日 09:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定