我的程序旨在通过用户的输入计算出矩形的面积。我的代码提示用户输入两次长度和宽度,第二次是程序将用于计算的值。数学都是正确的,(我知道)发生的唯一问题是重复输入提示。import java.util.Scanner;public class AreaRectangle{public static void main(String[] args){ double length=0; double width=0; double area; getLength(length); length= getLength(length); getWidth(width); width= getWidth(width); getArea(length,width); area= getArea(length, width); displayData(length, width, area); } public static double getLength(double length) { Scanner keyboard= new Scanner(System.in); double result; System.out.println("Enter the Rectangle's Length"); result= keyboard.nextDouble(); return result; } public static double getWidth(double width) { Scanner keyboard= new Scanner(System.in); double result; System.out.println("Enter the Rectangle's Width"); result=keyboard.nextDouble(); return result; } public static double getArea(double length, double width) { double result; result= (length*width); return result; } public static void displayData(double length, double width, double area) { System.out.println("The length is "+length+". The width is "+width); System.out.println("The area is "+area); }}
2 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
//你已经调用了每个方法两次
// getLength(length);
length = getLength(length);
// getWidth(width);
width = getWidth(width);
// getArea(length, width);
area = getArea(length, width);
displayData(length, width, area);
弑天下
TA贡献1818条经验 获得超8个赞
这是因为您要调用getLength()和getWidth()方法两次。修复非常简单,只需删除每个的一个调用。
public static void main(String[] args)
{
double length=0;
double width=0;
double area;
// getLength(length); not required
length= getLength(length);
// getWidth(width); not required
width= getWidth(width);
添加回答
举报
0/150
提交
取消