我是一名新程序员,我收到的错误在我的ShapeApp类中找不到符号。 我的错误是
System.out.println(“矩形的当前长度为:”+ r1.getLength()); ^ 符号:变量r1 location:class ShapeApp
请尝试以更简单的方式解释它。非常感谢,我的代码如下。
public class Rectangle
{
protected double length;
protected double width;
public Rectangle() { }
public Rectangle(double l,double w)
{
length = l;
width = w;
}
public void setLength(double l) {length = l;}
public double getLength() {return length;}
public void setWidth(double w) {width = w;}
public double getWidth() {return width;}
public double findArea() {return length * width;}
public String toString()
{
return "\tLength " + length + "\tWidth " + width;
}
}
public class Box extends Rectangle
{
private double height;
public Box() { }
public Box(double l,double w,double h)
{
super(l,w);
height = h;
}
public void setHeight(double h) {height = h;}
public double getHeight() {return height;}
public double findArea() {return ((super.findArea() * 2) + (2 * height * width) + (2 * height * length));}
public double findVolume() {return super.findArea() * height;}
public String toString()
{
return super.toString() + "\tHeight " + height;
}
}
import java.util.*;
public class ShapeApp
{
public static void main(String[] args)
{
Rectangle r1 = new Rectangle(20,10);
Box b1 = new Box(10,5,5);
int options;
Scanner input = new Scanner(System.in);
do{
displayMenu();
options = input.nextInt();
switch(options)
{
case 1: changeRecL();
break;
case 2: changeBoxL();
break;
case 3: changeBoxH();
break;
case 4: displayAreaRec();
break;
case 5: displaySaBox();
break;
case 6: displayVoBox();
break;
case 0: System.out.println("Exiting Program");
break;
default: System.out.println("Invalid Option. ");
}
}while(options != 0);
}
public static void displayMenu()
{
System.out.println("-------------------------------MENU-------------------------------");
System.out.println("[1] Change the length of rectangle");
System.out.println("[2] Change the length of box");
System.out.println("[3] Change the height of box");
System.out.println("[4] Display the area of rectangle");
System.out.println("[5] Display the surface area of box");
System.out.println("[6] Display the volume of box");
System.out.println("[0] Exit");
System.out.println("------------------------------------------------------------------");
System.out.println("Enter your option:");
}
public static void changeRecL()
{
Scanner input = new Scanner(System.in);
System.out.println("Current length of rectangle is: " + r1.getLength());
System.out.println("Enter new length of rectangle: ");
double nlength = input.nextDouble();
r1.setLength(nlength);
}
public static void changeBoxL()
{
}
public static void changeBoxH()
{
}
public static void displayAreaRec()
{
}
public static void displaySaBox()
{
}
public static void displayVoBox()
{
}
}
答案 0 :(得分:1)
那是因为你没有在你的方法changeRecL中定义r1。
也许您想将r1从main传递给您的方法,如下所示:
case 1: changeRecL(r1);
并接受R1如下:
public static void changeRecL(Rectangle r1)
答案 1 :(得分:0)
定义对象类时,可以将类的多个实例作为对象。不仅有一个矩形r1。如果你只想为一切使用一个矩形,你应该实现一个Singleton类。
public class Rectangle {
double len;
double wid;
private static Rectangle instance = null;
protected Rectangle() {
// Exists only to defeat instantiation.
}
public static Rectangle getInstance() {
if(instance == null) {
instance = new Rectangle();
}
return instance;
}
public static void setLen (double l){
len = l;
}
public static double getLen (){
return len;
}
public static void setWid (double w){
wid = w;
}
public static double getWid(){
return wid;
}
}
但是,就像@almas_shaikh所说的那样,将对象实例传递给方法更容易。
另外,让我提醒您,java length
一词用于确定数组和其他对象的大小,如getLength()
方法。您应该使用另一个名称来设置长度属性和方法以避免冲突。