myRectangle.calculateArea(); returns 错误 "package myRectangle does not exist"
myRectangle.calculateArea(); returns error "package myRectangle does not exist"
矩形
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
}
我正在尝试创建一个程序“Rectangle”来计算矩形的面积和周长,然后创建一个测试程序来 运行 程序“Rectangle”
我从之前的一个名为“Date”的作业中复制了代码,其中的基本思想是相似的,但是当我到达程序末尾时我需要调用“calculateArea();”和“计算周长();”在测试程序中,我收到一条错误消息,告诉我包 myRectangle 不存在……有人能告诉我为什么会这样吗?一个类似的代码在之前的作业中起作用,我发现其他人的代码用于相同的“Rectangle”程序并且它显示相同的错误。是我做错了什么还是我的 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,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 这样的
矩形
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
}
我正在尝试创建一个程序“Rectangle”来计算矩形的面积和周长,然后创建一个测试程序来 运行 程序“Rectangle” 我从之前的一个名为“Date”的作业中复制了代码,其中的基本思想是相似的,但是当我到达程序末尾时我需要调用“calculateArea();”和“计算周长();”在测试程序中,我收到一条错误消息,告诉我包 myRectangle 不存在……有人能告诉我为什么会这样吗?一个类似的代码在之前的作业中起作用,我发现其他人的代码用于相同的“Rectangle”程序并且它显示相同的错误。是我做错了什么还是我的 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,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 这样的