为什么 JOptionPane.showInputDialog 会出现 "void could not be converted to int" 错误?

Why am I getting a "void could not be converted to int" error with JOptionPane.showInputDialog?

我在学校有这个作业,我必须使用 JOptionPane 来计算不同形状的面积,但是当我使用 JOptionPane.showMessageDialog 时,它变成红色并且错误代码显示 void 无法转换为 int。有人可以帮我弄清楚如何使用 JOptionPane.showMessageDialog 吗?

public static void areaTriangle(int base, int height)
{
    System.out.println(0.5 * base * height);
}

public static void areaCircle(int radius, double area)
{
    area = Math.PI * (radius * radius);
    System.out.println(area);
}

public static void areaRectangle(int length, int width)
{
    System.out.println(length * width);
}

public static void calcArea()
{
    System.out.println("What type of area do you want to calculate?");
    inputStr = JOptionPane.showInputDialog("Type 1 for triangle, 2 for circle, 3 for rectangle, and 0 for none, then press ENTER.");
    int type = Integer.parseInt(inputStr);

    if (type == 0)
    {
        return;

    }
    else if (type == 1)
    {
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the base, then press ENTER.");
        int base = Integer.parseInt(inputStr);
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the height, then press ENTER.");
        int height = Integer.parseInt(inputStr);
        int areaT = areaTriangle (base, height);
        JOptionPane.showMessageDialog(null, "The area of the triangle is: " + areaT);

    }
    else if (type == 2)
    {
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the radius, then press ENTER.");
        int radius = Integer.parseInt(inputStr);
        double area = Math.PI * (radius * radius);
        int areaC = areaCircle (radius, area);
        JOptionPane.showMessageDialog(null, "The area of the cicle is: " + areaC);

    }
    else if (type == 3)
    {
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the length, then press ENTER.");
        int length = Integer.parseInt(inputStr);
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the width, then press ENTER.");
        int width = Integer.parseInt(inputStr);
        int areaR = areaRectangle(length, width);
        JOptionPane.showMessageDialog(null, "The area of the rectangle is: " + areaR);
    }

}

public static void main(String[] args)
{
    calcArea();
}

您的问题与 JOptionPane 无关,而与您的 area* 方法有关

首先你将所有的方法声明为 void...

public static void areaTriangle(int base, int height) {
    System.out.println(0.5 * base * height);
}

public static void areaCircle(int radius, double area) {
    area = Math.PI * (radius * radius);
    System.out.println(area);
}

public static void areaRectangle(int length, int width) {
    System.out.println(length * width);
}

但是当您调用它们时,您尝试将 return 结果分配给变量...

int areaT = areaTriangle(base, height);
//...
int areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);

这根本没有意义。

根据您似乎想要做的事情,您需要将 area* 方法更改为 return 某种值...

public static double areaTriangle(int base, int height) {
    return 0.5 * base * height;
}

public static double areaCircle(int radius, double area) {
    return Math.PI * (radius * radius);
}

public static int areaRectangle(int length, int width) {
    return length * width;
}

然后您可以将方法中的 return 值分配给一些变量...

double areaT = areaTriangle(base, height);
//...
double areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);