为程序添加验证 - JAVA

时间:2015-12-02 17:48:24

标签: java swing

我编写了一个程序代码,要求用户选择一个形状,为其收音机或宽度设置一个数字,然后打印出形状区域。我正在尝试添加一些验证,例如,用户必须为圆的半径设置10-30之间的数字,并且应用程序继续提示用户,直到他/她输入有效数字。到目前为止我还没能做到。有帮助吗?这就是我到目前为止所做的:

package shapes;

import javax.swing.JOptionPane;

public class Shapes {

public static void main(String[] arg) {
    mainWindow();
}

private static void mainWindow() {
    int more = 0;
    String[] buttons = {"Circle", "Rectangle", "Triangle"};
    while (more == 0) {

        int userChoice = JOptionPane.showOptionDialog(null,
                "Select a Shape", "mainWindow Task 1", 
                1, 3, null, buttons, buttons[2]);

        if (userChoice == 0) {
            userCircle(); 
        } else if (userChoice == 1) {
            userRectangle(); 
        } else if (userChoice == 2) {
            userTriangle(); 
        } else {
            System.exit(-1);
        }

        more = JOptionPane.showConfirmDialog(null,
            "Any other Shape?", "mainWindow Task 1", 2, 3);
    }
}

private static void userCircle() {
    String enteredRadius = JOptionPane.showInputDialog(null,
            "Enter a number greater than 0 for the radius of the circle. ", "1");
    double radius = Double.parseDouble(enteredRadius);
    Circle userCircle = new Circle(radius);
    double area = userCircle.getArea();
    double perimeter = userCircle.getCircumference();
    JOptionPane.showMessageDialog(null,
            "A circle with the radius of " + unitTest(radius) + " has\n"
            + "an area of " + unitTest(area) + ",\n"
            + "and a perimeter = " + unitTest(perimeter) + ".");
}


private static void userRectangle() {
    String enteredLength = JOptionPane.showInputDialog(null,
            "Enter a number greater than 0 for the length of the rectangle. ", "1");
    String enteredWidth = JOptionPane.showInputDialog(null,
            "Enter a number greater than 0 for the width of the rectangle. ", "1");
    double length = Double.parseDouble(enteredLength);
    double width = Double.parseDouble(enteredWidth);
    Rectangle userRectangle = new Rectangle(length, width);
    double area = userRectangle.getArea();
    double perimeter = userRectangle.getPerimeter();
    JOptionPane.showMessageDialog(null,
            "A rectangle with a length of " + unitTest(length) + ", and a width of " + unitTest(width) + ",\n"
            + "has an area of " + unitTest(area) + ",\n"
            + "and a perimeter " + unitTest(perimeter) + ".");
}

private static void userTriangle() {
    String userSideA = JOptionPane.showInputDialog(null,
            "Enter a number greater than 0 for the length for side a. ", 3);
    String userSideB = JOptionPane.showInputDialog(null,
            "Enter a number greater than 0 for the length for side b. ", 4);
    String userSideC = JOptionPane.showInputDialog(null,
            "Enter a number greater than 0 for the length for side c. ", 5);
    double sideA = Double.parseDouble(userSideA);
    double sideB = Double.parseDouble(userSideB);
    double sideC = Double.parseDouble(userSideC);
    Triangle userTriangle = new Triangle(sideA, sideB, sideC);
    double area = userTriangle.getArea();
    double perimeter = userTriangle.getPerimeter();
    JOptionPane.showMessageDialog(null, "A triangle with\n"
            + "a side \"A\" of " + unitTest(sideA) + ",\n"
            + "a side \"B\" of " + unitTest(sideB) + ",\n"
            + "a side \"C\" of " + unitTest(sideC) + ",\n"
            + "has an area of " + unitTest(area)
            + ", and a perimeter of " + unitTest(perimeter));
}

private static String unitTest(double userEntry) {
    if (userEntry == 1) {
        return userEntry + " unit";
    } else {
        return userEntry + " units";
    }
   }
}

1 个答案:

答案 0 :(得分:1)

您可以修改if (item.ItemClass.StartsWith("IPM.Schedule.Meeting.Request")) { fileName = exportDirectory + "download.dat"; var meetingRequest = MeetingRequest.Bind(service, item.Id, props); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { fs.Write(meetingRequest.MimeContent.Content, 0,meetingRequest.MimeContent.Content.Length); } } 方法,直到输入正确为止,并使用userCircle循环,例如:

do-while

但是有更好的方法可以做到这一点,例如JSpinner和@ {3}}中提到的comment

我还建议您在应用中添加1个(不再是)JFrame,因此,只要您private static void userCircle() { double radius = 0; double area = 0; double perimeter = 0; do { String enteredRadius = JOptionPane.showInputDialog(null, "Enter a number greater than 0 for the radius of the circle. ", "1"); radius = Double.parseDouble(enteredRadius); area = 3.14 * radius * radius; //Change for your Circle.getArea() method perimeter = 3.14 * 2 * radius; //Change for your Circle.getPerimeter() method } while (radius < 10 || radius > 30); JOptionPane.showMessageDialog(null, "A circle with the radius of " + unitTest(radius) + " has\n" + "an area of " + unitTest(area) + ",\n" + "and a perimeter = " + unitTest(perimeter) + "."); } ,就可以看到正在运行的应用。

试一试,看看它是如何运作的。另外,我可能会建议您发布一个Runnable Example几乎发布一个,但它有ALT+TABCircleTriangle个类,你没有发布在这里......