java大题,走过的来看一看。/,。?[,],[,@#$%^&*()__+}|aksldha;ljgdhas;likdhsa
1、
class Square {
private double length;
private double width;
public Square(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
public double getCircumference() {
return 2 * (length + width);
}
}
2、
public class MySquare {
public static void main(String[] args) {
double length = Double.parseDouble(args[0]);
double width = Double.parseDouble(args[1]);
Square s = new Square(length, width);
if (length >= 20 && length <= 50 && width >= 60 && width <= 90) {
System.out.println("The area of the square with length " + length + " cm and width " + width + " cm is " + s.getArea() + " cm^2.");
} else if (length >= 70 && length <= 100 && width >= 30 && width <= 50) {
System.out.println("The circumference of the square with length " + length + " cm and width " + width + " cm is " + s.getCircumference() + " cm.");
} else {
System.out.println("Invalid input.");
}
}
}
以下是完成上述任务的 Java 代码:
Square.java
Copy code
public class Square {
private double length; // 长方形长
private double width; // 长方形宽
public Square(double length, double width) {
this.length = length;
this.width = width;
}
// 求长方形的面积的方法
public double getArea() {
return length * width;
}
// 求长方形的周长的方法
public double getCircum() {
return 2 * (length + width);
}
}
MySquare.java
Copy code
import java.util.Scanner;
public class MySquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 从命令行参数输入长方形长和宽
System.out.print("请输入长方形长:");
double length = input.nextDouble();
System.out.print("请输入长方形宽:");
double width = input.nextDouble();
Square square = new Square(length, width);
// 当长方形长在[20,50]区间,宽在[60,90]区间时,求长方形的面积
if (length >= 20 && length <= 50 && width >= 60 && width <= 90) {
System.out.println("长方形面积为:" + square.getArea());
}
// 当长方形长在[70,100]区间,宽在[30,50]区间时,求长方形的周长
if (length >= 70 && length <= 100 && width >= 30 && width <= 50) {
System.out.println("长方形周长为:" + square.getCircum());
}
}
}
在运行 MySquare 程序时,需要从命令行参数输入长方形长和宽。例如,输入长方形长为 35,宽为 70,在命令行中运行程序的命令如下:
Copy code
java MySquare 35 70
希望上述内容能够帮助您解决问题。如果在运行 MySquare 程序时遇到了问题,可以尝试以下方法解决:
检查程序代码是否有语法错误。Java 程序中如果出现语法错误,编译器会报错,无法运行。可以使用 Java 编译器(如 javac)检查程序代码是否有语法错误。
检查是否在正确的目录下运行程序。Java 程序需要在包含源代码文件的目录下运行,否则会报找不到类的错误。
确保已经正确安装了 Java 环境。如果没有安装 Java 环境,可以参考 Java 官网上的安装说明进行安装。
如果以上方法都无法解决问题,可以尝试在程序代码中加入打印调试信息的代码,以便于找出问题的所在。
希望这些建议能够帮助您解决问题。