java——方法在类外调用时返回正确的值,但在从类内调用时不返回正确的值?

flvtvl50  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(334)

我在做java的基础知识,所以在做一个更大的程序时,我被困在了这个东西上,我想我会尝试在一个单独的程序中修复它。但我还是很困惑。我把这个问题分解成一件我无法解释的事情,那就是代码。

package Basics;

import java.util.Scanner;
import java.util.Arrays;

public class Polygon {

    Scanner input = new Scanner(System.in);
    int num;

    Polygon(int n) {
        this.num = n;
    }

    int[] getSides() {
        int[] sides = new int[this.num];
        for (int i = 0; i < this.num; i++) {
            System.out.println("Enter length of side " + (i + 1));
            sides[i] = input.nextInt();
        }
        //System.out.println(Arrays.toString(sides));
        return sides;
    }

    int[] sides = this.getSides();

}

class runner {

    public static void main(String[] args) {
        Polygon rectangle = new Polygon(4);
        int[] sides = rectangle.getSides();
        System.out.println(Arrays.toString(sides));
        System.out.println(Arrays.toString(rectangle.sides));

    }

}

这是输出

run:
Enter length of side 1
5
Enter length of side 2
4
Enter length of side 3
3
Enter length of side 4
2
[5, 4, 3, 2]
[]
[]
BUILD SUCCESSFUL (total time: 3 seconds)

因此,当从类外部调用时,可以看到getsides()方法返回正确的值,即用户输入的值。但从内部看不是吗?我很确定这是一个愚蠢的问题,我遗漏了一些重要的东西,但是我真的需要帮助来解决这个问题,我该怎么解决?

kgsdhlau

kgsdhlau1#

在功能上 getSides() 您定义了一个新的 sides ,您没有调用您定义为的类示例变量 int[] sides = this.getSides(); ```
int[] getSides() {
sides = new int[this.num];
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
// System.out.println(Arrays.toString(sides));
return sides;
}

完整代码:

import java.util.Arrays;
import java.util.Scanner;

public class Polygon {
Scanner input = new Scanner(System.in);
int num;

Polygon(int n) {
    this.num = n;
}

int[] getSides() {
    this.sides = new int[this.num];
    for (int i = 0; i < this.num; i++) {
        System.out.println("Enter length of side " + (i + 1));
        sides[i] = input.nextInt();
    }
    // System.out.println(Arrays.toString(sides));
    return sides;
}

int[] sides = this.getSides();

}

跑步者等级:

public class Runner {
public static void main(String[] args) {
Polygon rectangle = new Polygon(4);
int[] sides = rectangle.getSides();
System.out.println(Arrays.toString(sides));
System.out.println(Arrays.toString(rectangle.sides));
}
}

为了得到更好的代码,您可以编写 `Polygon` 等级如下:

public class Polygon {
Scanner input = new Scanner(System.in);
int num;
int[] sides;

Polygon(int n) {
    this.num = n;
    this.sides = new int[this.num];
}

int[] getSides() {
    for (int i = 0; i < this.num; i++) {
        System.out.println("Enter length of side " + (i + 1));
        sides[i] = input.nextInt();
    }
    System.out.println(Arrays.toString(sides));
    return sides;
}

}

ktecyv1j

ktecyv1j2#

public class Polygon {
    Scanner input = new Scanner(System.in);
    int num;    // Java default value for integers is zero

    Polygon(int n) {
        this.num = n; // The value is changed when an object is created;
    }

    int[] getSides() {
        int[] sides = new int[this.num]; 
        for (int i = 0; i < this.num; i++) {
            System.out.println("Enter length of side " + (i + 1));
            sides[i] = input.nextInt();
        }
        return sides;
    }

    int[] sides = this.getSides(); // This method call to getSides will be executed before the Constructor, using all the instance variables(so, the variable num is 0)
}

您的问题是,您需要为调用方法getsides()的变量赋值,因为这个赋值将在构造函数之前运行,所以变量num是0(默认值),所以不进行迭代。
在构造函数之前执行的示例方法

public class Polygon {

    Scanner input = new Scanner(System.in);
    int num;
    int[] sides = this.getSides();  // Assign a variable with  the return of a method, this is executed before the constructor

    Polygon(int n) {
        System.out.println("I am the constructor");
        this.num = n;
    }

    int[] getSides() {
        System.out.println("I will be executed before the constructor");
        int[] sides = new int[this.num];
        for (int i = 0; i < this.num; i++) {
            System.out.println("Enter length of side " + (i + 1));
            sides[i] = input.nextInt();
        }
        //System.out.println(Arrays.toString(sides));
        return sides;
    }

    public static void main(String[] args) {
        new Polygon(5);
    }

相关问题