【发布时间】:2021-10-01 18:25:00
【问题描述】:
试图创建一个测试类,但我似乎无法连接它们。有任何想法吗?我一直gettign的错误
错误:在类 Rectangle 中找不到主方法,请将主方法定义为: 公共静态无效主要(字符串 [] 参数) 或者 JavaFX 应用程序类必须扩展 javafx.application.Application
import java.util.Scanner;
class Rectangle {
private double length;
private double width;
//constructors
//set width and length to one as defaults
public void Rectangle() {
length = 1;
width = 1;
}
public void Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
//Set for length and width
public void setLength(double length) {
if(length > 0.0 || length < 20.0) {
this.length = length;
}else{
throw new IllegalArgumentException("Please choose between 0.0 and 20.0");
}
}
public void setWidth(double width){
if(width > 0.0 || width < 20.0) {
this.width = width;
}else{
throw new IllegalArgumentException("Please choose between 0.0 and 20.0");
}
}
//Get for length and width
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
//Methods to calculate
public double getPerimeter() {
return (length * 2) + (width * 2);
}
public double getArea() {
return length * width;
}
public void showRec() {
System.out.println("Length: " + getLength() + "\nWidth: " + getWidth() + "\nArea: " + getArea() + "\nPerimeter: " + getPerimeter());
}
}
class TestRectangle{
public static void main(String[] args) {
System.out.println("Skylar Jackson - Assignment 7\n");
Scanner input = new Scanner(System.in);
double length;
double width;
System.out.print("1. Set Length\n2. Set Width\n3. Exit\nChoice: ");
int number = input.nextInt();
try {
Rectangle rec = new Rectangle();
switch(number) {
case 1:
System.out.println("Length:");
input.nextDouble();
rec.setLength(length);
break;
case 2:
System.out.println("Width:");
input.nextDouble();
rec.setWidth(width);
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Unknown Option");
break;
}
rec.showRec();
}catch(IllegalArgumentException e) {
System.out.println("The Length or Width needs to be between 0.0 and 20.0");
}
}//end main
} //end test
【问题讨论】:
-
听起来你运行的是
java Rectangle而不是java TestRectangle。
标签: java