java - 如何让JOptionPane计算面积?

标签 java

我已经创建了这段代码,GUI 弹出并且工作正常,但面积计算不正确。有什么线索吗?我对 Java 编码非常陌生,因此非常感谢您的帮助。提前致谢。

package pkg4.pkg2.pkgnew.project;

import javax.swing.JOptionPane;

public class NewProject {

    public static void main(String[] args) {

        String inputStr = JOptionPane.showInputDialog("Type 1 for the area of Triangle, 2 for area of Circle, 3 for Rectangle, and 0 for area of none of these.");

        int i = Integer.parseInt(inputStr);
        if (i == 1) {
            String input = JOptionPane.showInputDialog("Enter the first value to calculate the area of a triangle: ");
            int n1 = Integer.parseInt(inputStr);
            String inp = JOptionPane.showInputDialog("Enter the second value to calculate the area of a triangle: ");
            int n2 = Integer.parseInt(inputStr);
            areaTriangle(n1, n2);
        }
        if (i == 2) {
            String inpu = JOptionPane.showInputDialog("Enter a value to calculate the area of a circle: ");
            double radius = Integer.parseInt(inputStr);
            areaCircle(radius);
        }
        if (i == 3) {
            String inp = JOptionPane.showInputDialog("Enter the first value to calculate the area of a rectangle: ");
            int m1 = Integer.parseInt(inputStr);
            String inp2 = JOptionPane.showInputDialog("Enter the second value to calculate the area of a rectangle: ");
            int m2 = Integer.parseInt(inputStr);
            areaRectangle(m1, m2);
        } else {
            return;
        }
    }

    public static void areaTriangle(int n1, int n2) {
        int areat = (n1 * n2) / 2;
        JOptionPane.showMessageDialog(null, "The area of a triangle with your values is: " + areat);
    }

    public static void areaCircle(double radius) {
        double areac = Math.PI * (radius * radius);
        JOptionPane.showMessageDialog(null, "The area of a circle with your value is: " + areac);
    }

    public static void areaRectangle(int m1, int m2) {
        int arear = (m1 * m2);
        JOptionPane.showMessageDialog(null, "The area of a rectangle with your values is: " + arear);

    }

    public static void calcArea(int x) {

    }
}

最佳答案

代码的问题在于,每次将输入解析为字符串时,您始终使用该字符串的相同值。每次调用函数时,您都会在区域函数调用中使用所有 1、2 或 3 作为参数。因此,您需要更改 Integer.parseInt() 以包含从用户那里获得的新字符串,如下所示:

 String input = JOptionPane.showInputDialog("Enter the first value to calculate the area of a triangle: ");
 int n1 = Integer.parseInt(input); //not inputStr <----------
 String inp = JOptionPane.showInputDialog("Enter the second value to calculate the area of a triangle: ");
 int n2 = Integer.parseInt(inp);//not inputStr <---------
 areaTriangle(n1, n2);

关于java - 如何让JOptionPane计算面积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002784/

相关文章:

java - 删除 Android 工具栏的左边距

java - 如何将 JSlider 的值分配给变量的值

java - 我必须创建两种方法,一种用于顺时针旋转,另一种用于逆时针旋转

java - 将我的本地maven存储库同步到Nexus公共(public)存储库

java - 返回 JSONObject,而 JSONArray 无法在 Amazon Lambda Java 函数中工作

JavaFX:无法从匿名类调用应用程序的标签字段?

java - 你如何在 java 中缩短 system.out.println

java - Hibernate子表更新父对象

java - 通过 java 驱动程序将数据从 MongoDB 转换为原生 MATLAB 格式

java - Memcached 与 Hibernate 的集成