程序多次提示输入方法

时间:2018-10-22 04:47:37

标签: java methods return return-value

我的程序旨在通过用户输入找出矩形区域。我的代码提示用户输入两次长度和宽度,第二次是程序将用于计算的值。数学完全正确,(我知道)唯一正在发生的问题是重复输入提示。


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 个答案:

答案 0 :(得分:0)

//您两次调用了每种方法

  //  getLength(length);
    length = getLength(length);

   // getWidth(width);
    width = getWidth(width);

   // getArea(length, width);
    area = getArea(length, width);

    displayData(length, width, area);

答案 1 :(得分:0)

这是因为您要两次调用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);