我正在嘗試使用構造函數創建一個包含兩個類的程序。它必須返回矩形區域和周邊的答案。我寫了這個程序,但我無法弄清楚如何讓它返回。我在創建第二個類和使用構造函數時遇到了問題。Java編程
import java.util.Scanner
public class RectangleCalc{
public static void main(String[] args) {
Rectangle myRect = new Rectangle(1.5, 2.3);
double Area;
double Perimeter;
greetUser(); // method call
userInput(); // method call
userGoodbye(); // method call
myRect.setLength(0);
myRect.setWidth(0);
Area = myRect.area();
Perimeter = myRect.perim();}
public static void greetUser(){
System.out.println("Welcome to the Rectangle Calculator");}
public static void userInput(){
System.out.println("This program will accept the user input of length and width to calculate the perimeter and area of a rectangle.");
System.out.println("Would you like to continue Y/N?");
System.out.println("Enter the width ");
System.out.println("Enter the length ");
}
public static void Results(double area, double pr, double width, double length){
Scanner input=new Scanner;
System.out.println ("The width you entered is:" + width);
System.out.println ("The length you entered is:" + length);
System.out.println ("The area of your rectangle is:" + area);
System.out.println ("The perimeter of your rectangle is:" + pr);
System.out.println ("Would you like to calculate another rectangle Y/N?");}
public static void userGoodbye(){
System.out.println ("Thank you for using the Rectangle Calculator. Goodbye!");}
Rectangle newRect = new Rectangle(10, 20);
}
class Rectangle{
public double width, length;
public double len, wid;
public void setWidth(double w) {
width = w;
}
public void setLength(double ln) {
length = ln;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public double area() {
double area;
area = length * width;
return area;
}
public double perim() {
double pr;
pr = (length * 2) + (width * 2);
return pr;
}
public Rectangle(double len, double wid) {
}
}
注意,「雙區域」和「雙週邊」應該是小寫 – jpdymond