【问题标题】:Issues with methods parameters...Java方法参数的问题...Java
【发布时间】:2016-11-01 01:11:30
【问题描述】:

@Rogue 这是我设置它的新方法......我目前收到“找不到符号”错误。我仔细检查并声明了我的对象(r​​ectangle4)。 任何想法为什么?再次感谢兄弟...

package rectanglewithprivatedatafields;

class RectangleTestFile
{
public static void main(String[] args)
{
    double lengths = rectangle4.getLengths();
    double widths = rectangle4.getAreas();

//rectangle object 4
RectangleWithPrivateDataFields rectangle4 = new RectangleWithPrivateDataFields();
System.out.println("\nUser Specified Rectangle 4");
System.out.println("Please enter the appropriates sizes for your rectangle: \n");
System.out.println(rectangle4.getWidths());
System.err.println(rectangle4.getLengths());
System.out.println(rectangle4.getPerimeters(rectangle4.getLengths(), rectangle4.getWidths())); 
System.out.println(rectangle4.getAreas(rectangle4.getLengths(),rectangle4.getWidths()));
 }//main
}

【问题讨论】:

  • 包括完整的错误信息。更好的是,阅读它们,看看你是否能弄清楚它们在告诉你什么!
  • 您没有在您的main 中的任何地方声明lengthswidths。这些变量在该范围内不存在。将double lengths = rectangle4.getLengths(); 放在主目录的顶部。 widths 类似
  • @trooper 非常感谢您的帮助
  • 如果我没记错的话Scanner会消耗System.in流,不要每次都重做

标签: java oop methods


【解决方案1】:

你指的是rectangle4.getPerimeters(rectangle4.getLengths(), rectangle4.getWidths())而不是rectangle4.getPerimeters(lengths, widths)

您的实用程序类没有任何私有成员变量,只有公共访问器方法。

【讨论】:

    【解决方案2】:

    lengthswidths 不是您的 main 方法中的变量,因此请制作它们:

    int lengths = rectangle4.getLengths();
    int widths = rectangle4.getWidths();
    

    由于这些方法读取输入,您应该只调用它们一次。另外据我记得Scanner 消耗System.in 流,所以我只会做一次。继续前进(为了面向对象编程),我会让构造函数接受Scanner 来构建自己,然后让方法返回我需要的值:

    //Simple, short class names are easier to work with
    public class Rectangle {
    
        //length and width is data, or "state" of our rectangle, so store it
        private final int length;
        private final int width;
    
        //Allow creating it without requiring input
        public Rectangle(int width, int length) {
            this.width = width;
            this.length = length;
        }
    
        //We create ourselves given a Scanner which we can take input from
        public Rectangle(Scanner input) {
            System.out.print("Length: ");
            this.length = input.nextDouble();
            System.out.print("Width: ");
            this.width = input.nextDouble();
        }
    
        //Calculate based around known information
        public double getArea() {
            return this.length * this.width;
        }
    
        //Calculate based around known information
        public double getPerimeter() {
            return 2 * (this.length + this.width);
        }
    
        //getter
        public int getLength() {
            return this.length;
        }
    
        //getter
        public int getWidth() {
            return this.width;
        }
    
    }
    

    因此像这样调用它:

    public static final void main(String... args) {
        Scanner input = new Scanner(System.in); //make once
        Rectangle rect = new Rectangle(input); //let our rectangle do all the work
        //Simple!
        System.out.println("Your rectangle has a width of " + rect.getWidth()
                + ", a length of " + rect.getLength()
                + ", the perimeter " + rect.getPerimeter()
                + ", and an area of " + rect.getArea());
    }
    

    这里的关键思想是数据和角色的分离——Rectangle 将管理信息和自身创建的所有方面;无论您是手动提供lengthwidth,还是为它提供获取该信息的方法(我们的Scanner)。 Rectangle 将存储它需要的信息并返回我们想要的任何其他信息(我们的方法)。这使得类的计算和使用变得非常简单(您将输入传递给它,现在您的工作是从外部角度完成的)。

    【讨论】:

    • like "double lengths = rectangle4.getLengths(); 我得到一个错误...它说“找不到符号”
    • 嗯,这意味着没有rectangle4变量
    • 我有 RectangleWithPrivateDataFields rectangle4 = new RectangleWithPrivateDataFields();
    • 代码已更新...抱歉耽搁了,我正在工作。
    • 有什么方法可以付钱给你来帮助我完成这项任务吗?我的处境很糟糕,我只是想学习这些材料。我的学校不提供导师,我的老师也没有在线学生。我的工作让我一直在路上,所以我搞砸了。
    猜你喜欢
    • 2018-06-15
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2015-12-26
    • 2016-11-20
    相关资源
    最近更新 更多