如何解决这些问题?

时间:2016-03-09 20:20:56

标签: java

我的代码中收到了四条错误消息。它指出在模式2和模式4中,dArea无法解析为变量。稍后在代码中,即使只为变量赋值,使用和返回,也会认为该变量是重复的。我该如何解决这些问题?

import javax.swing.JOptionPane;

public class Lab7
{
public static void main(String args [])
{
    // Created by Makayla Scull, period 4B

    String sMode;
    int iMode = 0;
    sMode = JOptionPane.showInputDialog("Menu: \n [Square: Press 1] \n [Rectangle: Press 2] \n [Circle: Press 3] \n [Triangle: Press 4] \n [Trapezoid: Press 5] \n [To Exit: Press 6]");
    iMode = Integer.parseInt(sMode);

    while(iMode == 0);
    {
        if (iMode == 1)
        {
            Geometry.methodSquare();
        }

        if (iMode == 2)
        {
            Geometry.methodRectangle(dArea);
        }

        if (iMode == 3)
        {
            Geometry.methodCircle();
        }

        if (iMode == 4)
        {
            Geometry.methodTriangle(dArea);
        }

        if (iMode == 5)
        {
            Geometry.methodTrapezoid();
        }

        if (iMode == 6)
        {
         break;
        }
    }
}

}

class Geometry
{
    public static void methodSquare()
    {
        String sLength;
        double dLength;
        double dArea;
        int exp = 2;
        JOptionPane.showMessageDialog(null, "You chose the square.");
        sLength = JOptionPane.showInputDialog("Enter the side length of the square.");
        dLength = Double.parseDouble(sLength);
        dArea = Math.pow(dLength, exp);
        JOptionPane.showMessageDialog(null, "The area of the square is " + dArea);
    }

    public static double methodRectangle(double dArea)
    {
        String sLength;
        String sWidth;
        double dLength;
        double dWidth;
        JOptionPane.showMessageDialog(null, "You chose the rectangle.");
        sLength = JOptionPane.showInputDialog("Enter the length of the rectangle.");
        sWidth = JOptionPane.showInputDialog("Enter the width of the rectangle.");
        dLength = Double.parseDouble(sLength);
        dWidth = Double.parseDouble(sWidth);
        double dArea = (dLength * dWidth);
        JOptionPane.showMessageDialog(null, "The area of the rectangle is " + dArea);
        return dArea;
    }

    public static double methodCircle()
    {
        String sRadius;
        double dRadius;
        int exp = 2;
        JOptionPane.showMessageDialog(null, "You chose the circle.");
        sRadius = JOptionPane.showInputDialog("Enter the radius of the circle.");
        dRadius = Double.parseDouble(sRadius);
        double dArea = Math.pow(dRadius, exp) * Math.PI;
        JOptionPane.showMessageDialog(null, "The area of the circle is " + dArea);
        return dArea;
    }

    public static double methodTriangle(double dArea)
    {
        String sBase;
        String sHeight;
        double dBase;
        double dHeight;
        JOptionPane.showMessageDialog(null, "You chose the triangle.");
        sBase = JOptionPane.showInputDialog("Enter the base length of the triangle.");
        dBase = Double.parseDouble(sBase);
        sHeight = JOptionPane.showInputDialog("Enter the height of the triangle.");
        dHeight = Double.parseDouble(sHeight);
        double dArea = (dBase * dHeight) / 2;
        JOptionPane.showMessageDialog(null, "The area of the triangle is " + dArea);
        return dArea;
    }

    public static void methodTrapezoid()
    {
        String sHeight;
        String sBase1;
        String sBase2;
        double dHeight;
        double dBase1;
        double dBase2;
        JOptionPane.showMessageDialog(null, "You chose the trapezoid.");
        sHeight = JOptionPane.showInputDialog("Enter the height of the trapezoid.");
        dHeight = Double.parseDouble(sHeight);
        sBase1 = JOptionPane.showInputDialog("Enter the length of the first base of the trapezoid.");
        dBase1 = Double.parseDouble(sBase1);
        sBase2 = JOptionPane.showInputDialog("Enter the length of the second base of the trapezoid.");
        dBase2 = Double.parseDouble(sBase2);
        double dArea = ((dBase1 + dBase2) * dHeight) / 2;
        JOptionPane.showMessageDialog(null, "The area of the trapezoid is " + dArea);
    }
}

1 个答案:

答案 0 :(得分:2)

  

在模式2和4中,dArea无法解析为变量

那是因为你从未定义过那个变量。你试着用它:

IAngularStatic

但是在那个范围内你没有定义它。在使用之前必须存在一个值。

  

相同的变量据说是重复的

您的方法将变量定义为方法的参数:

Geometry.methodRectangle(dArea);

然后还尝试在方法中再次声明它:

public static double methodRectangle(double dArea)

正如错误告诉您的那样,变量只能在任何给定范围内声明一次。如果您尝试在同一范围内使用相同名称的两个变量,那么编译器将无法知道您所指的是哪一个。