代码运行,但是没有给我正确的输出

问题描述 投票:1回答:2

我对编码还很陌生。我不确定如何在方法之间移动输入值。我尝试了不同的方法,但这总是给我一个错误。我也很好奇您将如何返回多个值。就像可以说的那样,如果不使用getLength()和getWidth(),则将返回一个名为getInput()的方法,同时返回长度和宽度。

    import java.util.Scanner;  // Needed for the Scanner class
/**
   This program calculates the area of a rectangle.
*/
public class Rectangle
{
   //main method
   public static void main(String[] args)
   {  
      float length = 0;
      float width = 0;
      float area = 0;


      getLength();
      getWidth();   
      calcArea(length, width);
      displayData(area);
   }

   //method 2
   public static float getLength()
   { 
      // Create a Scanner object to read from the keyboard.
      Scanner keyboard = new Scanner(System.in);

      System.out.print("Enter Length: ");
      float length = keyboard.nextFloat();

      return length;     

   }

   public static float getWidth()
   {
      // Create a Scanner object to read from the keyboard.
      Scanner keyboard = new Scanner(System.in);

      System.out.print("Enter Width: ");
      float width = keyboard.nextFloat(); 

      return width;

   }


   public static float calcArea(float length, float width)
   {
     float area = length*width;

     System.out.print("\nxxxx "+area);

     return area;

   }

   public static void displayData(float area)
   {
      System.out.print("\nThe area of the rectangle is "+area);
   }

}
java methods output
2个回答
1
投票

这应该有所帮助:

      length = getLength();
      width = getWidth();   
      area = calcArea(length, width);
      displayData(area);

0
投票

您的问题来自非void方法将返回值的事实。如果您不将这些值分配给任何变量,则Java不会对它们进行任何操作。

float length = 0;
float width = 0;
float area = 0;

// Values are retrieved, but must be assigned to variables
length = getLength();
width = getWidth();   
area = calcArea(length, width);

我敦促您尝试一些更直观的方法。我了解您是编码的新手,但请尝试解决此问题并查看它的用途。 OOP是Java的核心组件,通常最好在可能的情况下使用它。

Rectangle.java:

public class Rectangle
{

    private float length;
    private float width;

    public Rectangle(float length, float width)
    {
        this.length = length;
        this.width = width;
    }

    public float getArea()
    {
        return length * width;
    }

    public float getLength()
    {
        return length;
    }

    public float getWidth()
    {
        return width;
    }
}

以下内容必须放在单独的文件中。

主类别:

public static void main(String[] args)
{
    // Only open one stream at a time
    // Opening multiple without closing the previous may cause some problems in the future
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter Length: ");
    float length = keyboard.nextFloat();
    System.out.print("Enter Width: ");
    float width = keyboard.nextFloat();

    // Remember to close the stream when you are finished using it
    keyboard.close();

    // Create a new rectangle with the length and width that was given by the user
    Rectangle rect = new Rectangle(length, width);
    // Round the area to the nearest tenth of a decimal place and display results
    System.out.printf("The area of the rectangle is %.1f", rect.getArea());
}
© www.soinside.com 2019 - 2024. All rights reserved.