Rectangle.java 不会 Return 正确的长度和宽度
Rectangle.java Won't Return Correct Length and Width
我是 Whosebug 的新手,我正在为我的 class(特别是 CSC 202J)做一个项目,我快完成了,但我遇到了一个小问题- 输入功能无法正常工作。我对此进行了编程,使矩形的默认宽度和长度为 1.0,但是当我调用任一 get 方法时,我发现两个值都没有改变。我将提供我的代码(连同驱动程序 class 和作业要求,以及测试的输出 运行。
作业:
Create a class Rectangle. The class has attributes length and width, each
of which defaults to 1. Provide methods that calculate the perimeter and the area of the rectangle.
Provide set and get methods for both length and width. The set methods
should verify that length and width are each floating-point numbers greater than or equal to 0.0 and less than 20.0.
Write a program to test class Rectangle.
样本输出:
1. Set Length
2. Set Width
3. Exit
Choice: 1
Enter Length: 10
Length: 10.00
Width: 1.00
Perimeter: 22.00
Area: 10.00
1. Set Length
2. Set Width
3. Exit
Choice: 2
Enter Width: 15
Length: 10.00
Width: 15.00
Perimeter: 50.00
Area: 150.00
1. Set Length
2. Set Width
3. Exit
Choice: 1
Enter Length: 1
Length: 1.00
Width: 15.00
Perimeter: 32.00
Area: 15.00
1. Set Length
2. Set Width
3. Exit
Choice: 3
类:
import java.util.Scanner;
public class RectangleTest {
public static void main (String[] args)
{
int response = 0;
Scanner userInput = new Scanner(System.in);
Rectangle r = new Rectangle();
do{
System.out.println("1. Set Length");
System.out.println("2. Set Width");
System.out.println("3. Exit");
System.out.println("Choice: ");
response = userInput.nextInt();
if(response == 1)
{
System.out.println("Enter Length: ");
r.setLength(userInput.nextInt());
r.toString();
System.out.println("Length: " + r.getLength());
System.out.println("Width: " + r.getWidth());
System.out.println("Perimeter: " + r.perimeter());
System.out.println("Area: " + r.area());
}
if(response == 2)
{
System.out.println("Enter Width: ");
r.setWidth(userInput.nextInt());
r.toString();
System.out.println("Length: " + r.getLength());
System.out.println("Width: " + r.getWidth());
System.out.println("Perimeter: " + r.perimeter());
System.out.println("Area: " + r.area());
}
}
while(response != 3);
System.exit(0);
}
}
public class Rectangle {
private double length;
private double width;
public Rectangle(){
this.length = 1.0;
this.width = 1.0;
}
public double getLength(){
return this.length;
}
public double getWidth(){
return this.width;
}
public boolean setLength(double length){
if (length > 0.0 && 1 < 20.0){
return true;
}
return false;
}
public boolean setWidth(double width){
if(width > 0.0 && width < 20.0){
return true;
}
return false;
}
public double perimeter(){
return 2 * (this.length + this.width);
}
public double area(){
return this.getLength() * this.getWidth();
}
@Override
public String toString(){
return "Length: " + this.length +"\tWidth: " + this.width;
}
}
测试运行输出:
1. Set Length
2. Set Width
3. Exit
Choice:
1
Enter Length:
10
Length: 1.0
Width: 1.0
Perimeter: 4.0
Area: 1.0
1. Set Length
2. Set Width
3. Exit
Choice:
2
Enter Width:
14
Length: 1.0
Width: 1.0
Perimeter: 4.0
Area: 1.0
1. Set Length
2. Set Width
3. Exit
Choice:
3
如果这是我忽略的菜鸟错误,我深表歉意。
您从未在 setXXX()
方法中设置值。在您的代码中包含 this.width = width
和 this.length = length
。
public class Rectangle {
private double width;
private double length;
public boolean setWidth(double width) {
if(width >= 0.0 && width < 20.0) {
this.width = width; // <<< You need this line ...
return true;
} else return false;
}
public boolean setLength(double length) {
if(length >= 0.0 && length < 20.0) {
this.length = length; // <<< ... and this one, too!
return true;
} else return false;
}
}
要更改矩形 class 的值,您必须执行以下操作:
public void setwidth(double width){
if (width => 0.0 && width < 20.0){
this.width = width;
}
}
同样适用于长度。目前,您的代码 return 是一个布尔值,指示输入宽度是否在特定值范围内。设置方法不需要 return 任何东西。
我注意到您的代码中的另一个错误,在 setLength 方法中;
public boolean setLength(double length){
if (length > 0.0 && 1 < 20.0){
return true;
}
return false;
}
您是否打算在 if 语句中显示 if (length> 0.0 && 1 < 20.0)
的地方键入 length
?
另一个注意事项:在您的作业描述中指出宽度和长度需要为 "greater or equal than zero",因此在代码中 if 语句看起来像 if (width >= 0.0 && width < 20.0)
。注意大于或等于符号。
我是 Whosebug 的新手,我正在为我的 class(特别是 CSC 202J)做一个项目,我快完成了,但我遇到了一个小问题- 输入功能无法正常工作。我对此进行了编程,使矩形的默认宽度和长度为 1.0,但是当我调用任一 get 方法时,我发现两个值都没有改变。我将提供我的代码(连同驱动程序 class 和作业要求,以及测试的输出 运行。
作业:
Create a class Rectangle. The class has attributes length and width, each of which defaults to 1. Provide methods that calculate the perimeter and the area of the rectangle.
Provide set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers greater than or equal to 0.0 and less than 20.0.
Write a program to test class Rectangle.
样本输出:
1. Set Length
2. Set Width
3. Exit
Choice: 1
Enter Length: 10
Length: 10.00
Width: 1.00
Perimeter: 22.00
Area: 10.00
1. Set Length
2. Set Width
3. Exit
Choice: 2
Enter Width: 15
Length: 10.00
Width: 15.00
Perimeter: 50.00
Area: 150.00
1. Set Length
2. Set Width
3. Exit
Choice: 1
Enter Length: 1
Length: 1.00
Width: 15.00
Perimeter: 32.00
Area: 15.00
1. Set Length
2. Set Width
3. Exit
Choice: 3
类:
import java.util.Scanner;
public class RectangleTest {
public static void main (String[] args)
{
int response = 0;
Scanner userInput = new Scanner(System.in);
Rectangle r = new Rectangle();
do{
System.out.println("1. Set Length");
System.out.println("2. Set Width");
System.out.println("3. Exit");
System.out.println("Choice: ");
response = userInput.nextInt();
if(response == 1)
{
System.out.println("Enter Length: ");
r.setLength(userInput.nextInt());
r.toString();
System.out.println("Length: " + r.getLength());
System.out.println("Width: " + r.getWidth());
System.out.println("Perimeter: " + r.perimeter());
System.out.println("Area: " + r.area());
}
if(response == 2)
{
System.out.println("Enter Width: ");
r.setWidth(userInput.nextInt());
r.toString();
System.out.println("Length: " + r.getLength());
System.out.println("Width: " + r.getWidth());
System.out.println("Perimeter: " + r.perimeter());
System.out.println("Area: " + r.area());
}
}
while(response != 3);
System.exit(0);
}
}
public class Rectangle {
private double length;
private double width;
public Rectangle(){
this.length = 1.0;
this.width = 1.0;
}
public double getLength(){
return this.length;
}
public double getWidth(){
return this.width;
}
public boolean setLength(double length){
if (length > 0.0 && 1 < 20.0){
return true;
}
return false;
}
public boolean setWidth(double width){
if(width > 0.0 && width < 20.0){
return true;
}
return false;
}
public double perimeter(){
return 2 * (this.length + this.width);
}
public double area(){
return this.getLength() * this.getWidth();
}
@Override
public String toString(){
return "Length: " + this.length +"\tWidth: " + this.width;
}
}
测试运行输出:
1. Set Length
2. Set Width
3. Exit
Choice:
1
Enter Length:
10
Length: 1.0
Width: 1.0
Perimeter: 4.0
Area: 1.0
1. Set Length
2. Set Width
3. Exit
Choice:
2
Enter Width:
14
Length: 1.0
Width: 1.0
Perimeter: 4.0
Area: 1.0
1. Set Length
2. Set Width
3. Exit
Choice:
3
如果这是我忽略的菜鸟错误,我深表歉意。
您从未在 setXXX()
方法中设置值。在您的代码中包含 this.width = width
和 this.length = length
。
public class Rectangle {
private double width;
private double length;
public boolean setWidth(double width) {
if(width >= 0.0 && width < 20.0) {
this.width = width; // <<< You need this line ...
return true;
} else return false;
}
public boolean setLength(double length) {
if(length >= 0.0 && length < 20.0) {
this.length = length; // <<< ... and this one, too!
return true;
} else return false;
}
}
要更改矩形 class 的值,您必须执行以下操作:
public void setwidth(double width){
if (width => 0.0 && width < 20.0){
this.width = width;
}
}
同样适用于长度。目前,您的代码 return 是一个布尔值,指示输入宽度是否在特定值范围内。设置方法不需要 return 任何东西。
我注意到您的代码中的另一个错误,在 setLength 方法中;
public boolean setLength(double length){
if (length > 0.0 && 1 < 20.0){
return true;
}
return false;
}
您是否打算在 if 语句中显示 if (length> 0.0 && 1 < 20.0)
的地方键入 length
?
另一个注意事项:在您的作业描述中指出宽度和长度需要为 "greater or equal than zero",因此在代码中 if 语句看起来像 if (width >= 0.0 && width < 20.0)
。注意大于或等于符号。