微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

myRectangle.calculateArea();返回错误“包 myRectangle 不存在”

如何解决myRectangle.calculateArea();返回错误“包 myRectangle 不存在”

矩形

public class Rectangle {

  private double width;
  private double length; 


public Rectangle(double L,double W){
  length = L;
  width = W;
}

public void setLength(double Length){
  if (length>=0 && length <=20)
    length = Length;
else{
  length = 0;
}
}
public double getLength(){
  return length;
}
public void setWidth(double Width){
  if (width>=0 && length <=20)
    width = Width;
else{
    width = 0;
}
}
public double getWidth(){
  return width;
}

public void calculatePerimeter(){
  System.out.println("The perimeter of rectangle is: " + 2 * (length + width));
}
public void calculateArea(){
  System.out.println("The area of the rectangle is: " + (length * width));
}
}    
   

测试矩形

import java.util.Scanner;

public class TestRectangle {
public static void main(String[] args){

Scanner input = new Scanner(system.in);
  Rectangle myRectangle = new Rectangle (0,0);//I did the same thing in a 
//prevIoUs assignment,and someone else who did this assignment (code found 
//online) also did this. It worked before but seems useless Now?

System.out.println("Enter length: ");
  double L = input.nextDouble();
  System.out.println();

System.out.println("Enter width: ");
  double W = input.nextDouble();
  System.out.println();
}
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
//<identifier> expected,package myRectangle does not exist
}

我正在尝试创建一个程序“矩形”来计算矩形的面积和周长,然后创建一个测试程序来运行程序“矩形” 我复制了之前名为“Date”的作业的代码,其中的基本思想是相似的,但是当我到达程序的末尾时,我需要调用“calculateArea();”和“计算周长();”在测试程序中,我收到一个错误,告诉我包 myRectangle 不存在......有人能告诉我为什么会这样吗?类似的代码在之前的作业中有效,我找到了其他人为同一个“矩形”程序编写的代码,它显示了相同的错误。是我做错了什么还是我的 NetBeans 有问题?

这是我基于 Rectangle 和 TestRectangle 程序的代码 日期

public class Date {
private int month;
private int day;
private int year;

public Date(int m,int d,int y){
    month = m;
    day = d;
    year = y;
}

public void setMonth(int Months){
    if(Months>=0 && Months <= 12)
        month=Months;
    else{
        month=0;
    }
}
public int getMonth(){
    return month;
}

public void setDay(int Days){
    if(Days>= 0 && Days<=31)
        day = Days;
        else{
                day=0;
                }
}

public int getDay(){
    return day;
}

public void setYear(int Years){
    year=Years;
}
public int getYear(){
    return year;
}
public void displayDate(){
    System.out.printf
            ("%d/%d/%d\n",getMonth(),getDay(),getYear() );
}
}

测试日期

import java.util.Scanner;

public class DateTest {
public static void main(String[] args){
  Scanner input = new Scanner(system.in);

Date myDate = new Date(0,0);

System.out.println("Justine Dodge,assignment 6\n");


System.out.println("Please enter month: ");
  int m = input.nextInt();
  myDate.setMonth(m);
  System.out.println();


System.out.println("Enter day: ");
  int d = input.nextInt();
  myDate.setDay(d);//assign d to Day?
  System.out.println();//output blank line

System.out.println("Enter year: ");
  int y = input.nextInt();
  myDate.setYear(y);
  System.out.println();


myDate.displayDate();

}
}

解决方法

在 TestRectangle 中,您在调用这些函数之前关闭了 main 方法

} // this should be at at the end of the main function 
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error

myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
}

这将消除编译错误。现在,当您运行它时,您将获得面积和周长都为 0,因为在构造函数中,您将长度和宽度的值传递为 0,而不是真正使用作为输入的长度和宽度。 为了解决这个问题,而不是在构造函数中传递 0,0, 尝试像这样在构造函数中传递 L,W


  Rectangle myRectangle = new Rectangle (L,W);
myRectangle.getLength()); //
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();

import java.util.Scanner;

public class TestRectangle {
public static void main(String[] args){

Scanner input = new Scanner(System.in);
//  Rectangle myRectangle = new Rectangle (0,0);//I did the same thing in a 
//previous assignment,and someone else who did this assignment (code found 
//online) also did this. It worked before but seems useless now?

System.out.println("Enter length: ");
  double L = input.nextDouble();
  System.out.println();

System.out.println("Enter width: ");
  double W = input.nextDouble();
  System.out.println();

  
  Rectangle myRectangle = new Rectangle (L,W);
//  System.out.println("get length " + myRectangle.getLength());
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
//<identifier> expected,package myRectangle does not exist
}
}

那么你会得到这样的o/p enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。