java - 方法——调用方法

标签 java methods

import java.util.Scanner;
public class Rectangle
{
public static void main(String[] args)
{
    -Don't know how to call method here-
}

/**
 * returns the area of a rectangle
 * @param height the height of the rectangle
 * @param width the width of the rectangle
 */
public static int area (int length, int width)
{
    return length * width;
}

/**
 * returns the perimeter of a rectangle
 * @param height the height of the rectangle
 * @param width the width of the rectangle
 */
public static int perimeter (int length, int width)
{
    return length + width;
}

/**
 * returns the details of the rectangle
 * @param height the height of the rectangle
 * @param width the width of the rectangle
 */
public static void printRectangleDetails (int length, int width)
{
    System.out.println ("This is the length of the rectangle " + length);

    System.out.println ("This is the width of the rectangle " + width);

    System.out.println (("This is the perimeter of the rectangle " + (length + width)));

    System.out.println (("This is the area of the rectangle " + (length * width)));
}

/**
 * Read in an integer and return its value
 * @param the prompt to be shown to the user
 */
public static int readInteger(String prompt)
{
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");
    int input = scan.nextInt();
    return input;
}

我试图调用方法 readInteger 来提示用户插入矩形的高度和宽度。这是我第一次使用方法,所以任何帮助将不胜感激,我也不确定 readInteger 方法是否正确。

谢谢!

最佳答案

在您的 main() 方法中,您可以通过调用在 中创建的 readInteger() 方法来读取矩形的长度和宽度矩形类为:

 int length = readInteger(" For Length ");
 int width = readInteger("For Width ");
 printRectangleDetails(length,width);

首先,将这一行添加到 readInteger() 方法中:

  System.out.println (prompt);

关于java - 方法——调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13523241/

相关文章:

this.x = x; 的 C++ 非类作用域替换;

java - 每 x 分钟调用一个方法

list - 如何从 Scala 中的 hashmap 调用函数

c# - C# 静态方法模板

java - Mockito - 深度 stub

java - 我的按钮未显示在 Android Studio 中

Java:将数组列表转换为带有添加元素的数组

java - 模式中的规范等价

java - 在线 Java 的 JSON 解析器

java - 如何调用我的方法