无论怎么修改都会报错,请帮我看一看怎么改,谢谢!
代码如下:
import java.util.Scanner
public class Rectangle {
double width;
double heigth;
double area;
double computArea() {
area = width*heigth;
return area;
}
public static void main(String[] args) {
Scanner rectangle=new Scanner(System.in);
System.out.println("输入矩形的宽(按Enter键确认):");
double width=rectangle.nextDouble();
System.out.println("输入矩形的高(按Enter键确认):");
double heigth=rectangle.nextDouble();
System.out.println("矩形的宽:"+width);
System.out.println("矩形的高:"+heigth);
System.out.println("矩形的面积:"+rectangle.computArea(width,heigth));
}
}
public class Rectangle {
private static double width;
private static double heigth;
private static double area;
public static void main(String[] args) {
Scanner rectangle=new Scanner(System.in);
System.out.println("输入矩形的宽(按Enter键确认):");
width=rectangle.nextDouble();
System.out.println("输入矩形的高(按Enter键确认):");
heigth=rectangle.nextDouble();
System.out.println("矩形的宽:"+width);
System.out.println("矩形的高:"+heigth);
System.out.println("矩形的面积:"+computArea());
rectangle.close();
}
private static Double computArea() {
area = width*heigth;
return area;
}
}
computArea是Rectangle 类中的方法,不是Scanner 中的方法,你想要通过computArea方法计算面积,需要先new 出来Rectangle 然后调用computArea
cimputArea是你定义的一个方法,你不能把这个方法放在一个double类型变量下,你把这个方法放在外面Rectangle外面,并用public修饰。(undefined:尚未被定义的)