以下代码在运行后为我创建了一个错误,它投影了第一行“请输入矩形的长度”。正确,但是输入数字后的下一行是“输入矩形的长度:请输入矩形的宽度”。然后,在我第二次输入后,它将创建一个错误并使我的代码崩溃。澄清一下,我并不是要像那样将它们并排放置。任何帮助,将不胜感激。谢谢!
import java.util.Scanner;
public class AreaRectangle {
public static void main(String[] args) {
double length, // The rectangle's length
width, // The rectangle's width
area; // The rectangle's area
// Get the rectangle's length from the user.
length = getLength();
System.out.print("Enter the length of " +
"the rectangle: ");
// Get the rectangle's width from the user.
width = getWidth();
System.out.print("Enter the width of " +
"the rectangle: ");
// Get the rectangle's area.
area = getArea(length, width);
System.out.print("The area of the " +
"rectangle is: ");
// Display the rectangle data.
displayData(length, width, area);
System.out.print("Enter the length of " +
"the rectangle: ");
}
public static double getLength() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter length of a rectangle.");
double length = keyboard.nextDouble();
System.out.println("The length of the rectangle is " + length);
return length;
}
public static double getWidth() {
Scanner keyboard = new Scanner(System.in);
double width;
System.out.println("Please enter width of a rectangle.");
width = keyboard.nextDouble();
System.out.println("The width of the rectangle is " + width);
return width;
}
public static double getArea(double length, double width) {
double area = length * width;
System.out.println("The area of the rectangle is " + area);
return area;
}
public static double displayData(double length, double width, double
area) {
System.out.println("The length of the rectangle is: \t" + length);
System.out.println("The width of the rectangle is: \t" + width);
System.out.println("The area of the rectangle is: \t" + area);
}
}
答案 0 :(得分:0)
您忘记了从getLength函数返回值。
只需在其上添加return语句,它将起作用。
您已声明:
public static double displayData(....)
因此您必须返回一个双精度值或将函数声明更改为
public static void displayData
您的主要功能还有另一个问题:可以删除主作用域上的所有println。您也可以在getArea函数中删除println。您的代码如下:
import java.util.Scanner;
公共类AreaRectangle { 公共静态void main(String [] args){ double length,//矩形的长度 width,//矩形的宽度 区域; //矩形的区域
// Get the rectangle's length from the user.
length = getLength();
// Get the rectangle's width from the user.
width = getWidth();
// Get the rectangle's area.
area = getArea(length, width);
// Display the rectangle data.
displayData(length, width, area);
}
public static double getLength() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter length of a rectangle.");
double length = keyboard.nextDouble();
System.out.println("The length of the rectangle is " + length);
return length;
}
public static double getWidth() {
Scanner keyboard = new Scanner(System.in);
double width;
System.out.println("Please enter width of a rectangle.");
width = keyboard.nextDouble();
System.out.println("The width of the rectangle is " + width);
return width;
}
public static double getArea(double length, double width) {
double area = length * width;
return area;
}
public static void displayData(double length, double width, double
area) {
System.out.println("The length of the rectangle is: \t" + length);
System.out.println("The width of the rectangle is: \t" + width);
System.out.println("The area of the rectangle is: \t" + area);
}
}
,控制台输出就可以满足您的需求。
答案 1 :(得分:0)
main()
中删除所有提示。 return
语句keyboard.close();
之前关闭它。 答案 2 :(得分:0)
这是您的代码。看看它是如何实现的,然后尝试添加一些新功能,例如矩形的参数或对角线。并不难。
public class Main {
private static double lengthOfRectangle;
private static double widthOfRectangle;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input the length of the rectangle: \n");
lengthOfRectangle = scanner.nextDouble();
System.out.println("Input the length of the rectangle: \n");
widthOfRectangle = scanner.nextDouble();
System.out.printf("Length: %s - Width: %s - Area of your rectangle is %s", lengthOfRectangle,widthOfRectangle,lengthOfRectangle*widthOfRectangle);
}
}