| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 532 人关注过本帖
标题:初学时编的一个小程序,请帮忙简化一下!!!
取消只看楼主 加入收藏
myhnuhai
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:3
帖 子:425
专家分:1725
注 册:2010-3-17
结帖率:87.5%
收藏
 问题点数:0 回复次数:1 
初学时编的一个小程序,请帮忙简化一下!!!
import javax.swing.JOptionPane;

public class Synthesis {
    static final double PI = 3.14159;

    static String output = "";

    public static void main(String[] args) {
        String num = JOptionPane.showInputDialog("1:计算体积和面积\n" + "2:猜数字游戏\n"
                + "请输入要应用的相应的编号!");
        int m = Integer.parseInt(num);
        String output = "";
        switch (m) {
        case 1:
            Areaa();
            break;
        case 2:
            Guess();
            break;
        default:
            output = "请输入以上要计算的面积和体积相对应的编号,在(1-2)之间!";

        }
        JOptionPane.showMessageDialog(null, output);
    }

    public static void Areaa() {
        String number = JOptionPane.showInputDialog("1:三角形面积" + "\n" + "2:矩形面积"
                + "\n" + "3:梯形面积" + "\n" + "4:圆的面积" + "\n" + "5:球的面积及体积" + "\n"
                + "6:圆柱体的面积及体积" + "\n" + "请输入要计算的面积和体积的相应的编号!");
        int n = Integer.parseInt(number);
        switch (n) {
        case 1:
            Triangle();
            break;
        case 2:
            Rectangle();
            break;
        case 3:
            Trapizoid();
            break;
        case 4:
            Circle();
            break;
        case 5:
            Globe();
            break;
        case 6:
            Cylinderr();
            break;
        default:
            output = "请输入以上要计算的面积和体积相对应的编号,在(1-5)之间!";

        }
    }

    public static void Guess() {
        int count = 0;
        int nnm = (int) (10 + 90 * Math.random());
        for (int ii = 1; ii <= 100; ii++) {
            count++;
            String numberi = JOptionPane.showInputDialog("请输入100以内的一个整数 !");
            int mi = Integer.parseInt(numberi);
            if (mi <= 0 || mi >= 100) {
                output = "请输入1-100的整数!";
                JOptionPane.showMessageDialog(null, output);
                continue;
            } else if (mi < nnm) {
                output = "输入的值小于实际值!";
                JOptionPane.showMessageDialog(null, output);
                continue;
            } else if (mi > nnm) {
                output = "输入的值大于实际值!";
                JOptionPane.showMessageDialog(null, output);
                continue;
            } else {
                output = "恭喜你答对了!您最准得分为:" + (110 - count * 10);
                JOptionPane.showMessageDialog(null, output);
                break;
            }
        }
    }

    public static void Triangle() {
        double arr[] = new double[3];

        for (int i = 1; i <= 3; i++) {
            String numa = JOptionPane.showInputDialog("请输入三角形的第" + i + "条边!");
            double v = Double.parseDouble(numa);
            arr[i - 1] = v;
        }
        if (arr[0] == 0 || arr[1] == 0 || arr[2] == 0) {
            output = "您输入的数据错误," + "\n" + "三角形的边长不能为零," + "\n" + "请输入正确的数据 !";
        } else if (arr[0] + arr[1] <= arr[2] || arr[0] + arr[2] <= arr[1]
                || arr[1] + arr[2] <= arr[0])
            output = "您输入的数据必须是任意两边之和要大于第三遍," + "\n" + "请正确输入数据 !";
        else {
            double area = 0;
            double P = 0;
            P = (arr[0] + arr[1] + arr[2]) / 2;
            area = Math.pow(P * (P - arr[0]) * (P - arr[1]) * (P - arr[2]),
                    1.0 / 2);
            output = "三角形的面积为: " + area;
        }
    }

    public static void Rectangle() {
        int[] Arr1 = new int[2];
        for (int i = 1; i <= 2; i++) {
            String y = (i == 1) ? "宽" : "长";
            String num2 = JOptionPane.showInputDialog("请输入要计算的矩形的" + y);
            Arr1[i - 1] = Integer.parseInt(num2);
        }
        if (Arr1[0] == 0 || Arr1[1] == 0)
            output = "矩形的边长输入错误,请正确输入!";
        else {
            int tem = Arr1[0] * Arr1[1];
            output = "矩形的面积为:" + tem;
        }
    }

    public static void Trapizoid() {
        String numc = JOptionPane.showInputDialog("请输入梯形的底边长!");
        String numd = JOptionPane.showInputDialog("请输入梯形的上边长!");
        String nume = JOptionPane.showInputDialog("请输入梯形的高!");
        int A = Integer.parseInt(numc);
        int B = Integer.parseInt(numd);
        int H = Integer.parseInt(nume);
        if (A == 0 || B == 0 || H == 0)
            output = "请正确输入梯形的底边长,上边长以及高!";
        else {
            int tem1 = ((A + B) * H) / 2;
            output = "梯形的面积为:" + tem1;
        }
    }

    public static void Circle() {
        String num4 = JOptionPane.showInputDialog("请输入圆的半径!");
        int radius = Integer.parseInt(num4);
        if (radius == 0)
            output = "请正确输入圆的半径!";
        else {
            double tem2 = PI * Math.pow(radius, 2);
            output = "圆的面积为:" + tem2;
        }
    }

    public static void Globe() {
        String numg = JOptionPane.showInputDialog("请输入球的半径!");
        int radiu = Integer.parseInt(numg);
        if (radiu == 0)
            output = "请正确输入球的半径!";
        else {
            double CircleOfArea = 4 * PI * radiu * radiu;
            double CicleOfVomlue = (4.0 / 3) * PI * radiu * radiu * radiu;
            output = "球的表面积为:" + CircleOfArea + "\n" + "球的体积为:" + CicleOfVomlue;
        }
    }

    public static void Cylinderr() {
        String numberr = JOptionPane.showInputDialog("请输入圆柱体的底面半径 !");
        String number1 = JOptionPane.showInputDialog("请输入圆柱体的高!");
        int a = Integer.parseInt(numberr);
        int x = Integer.parseInt(number1);
        if (a == 0 || x == 0)
            output = "请正确输入圆柱体的底面半径和高!";
        else {
            output = "圆柱体的表面积为: " + Area(a, x) + "\n" + "圆柱体的体积为: "

            + Cylinder(a, x);
        }
    }

    public static double Cylinder(int circle, int height) {         final double PI = 3.14159;
        double volume = PI * Math.pow(circle, 2) * height;
        return volume;
    }

    public static double Area(int i, int k) {         final double PI = 3.14159;
        double surfaceArea = 2.0 * PI * i * k;
        return surfaceArea;
    }
}
搜索更多相关主题的帖子: 初学 
2010-05-13 15:07
myhnuhai
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:3
帖 子:425
专家分:1725
注 册:2010-3-17
收藏
得分:0 

一点小错误改进了一下:
import javax.swing.JOptionPane;

public class Synthesis {
    static final double PI = 3.14159;

    static String output = "";

    public static void main(String[] args) {
        String num = JOptionPane.showInputDialog("1:计算体积和面积\n" + "2:猜数字游戏\n"
                + "请输入要应用的相应的编号!");
        int m = Integer.parseInt(num);
        switch (m) {
        case 1:
            Areaa();
            
            break;
        case 2:
            Guess();
            break;
        default:
            output = "请输入以上要计算的面积和体积相对应的编号,在(1-2)之间!";

        }
        JOptionPane.showMessageDialog(null, output);
    }

    public static void Areaa() {
        String number = JOptionPane.showInputDialog("1:三角形面积" + "\n" + "2:矩形面积"
                + "\n" + "3:梯形面积" + "\n" + "4:圆的面积" + "\n" + "5:球的面积及体积" + "\n"
                + "6:圆柱体的面积及体积" + "\n" + "请输入要计算的面积和体积的相应的编号!");
        int n = Integer.parseInt(number);
        switch (n) {
        case 1:
            Triangle();
            break;
        case 2:
            Rectangle();
            break;
        case 3:
            Trapizoid();
            break;
        case 4:
            Circle();
            break;
        case 5:
            Globe();
            break;
        case 6:
            Cylinderr();
            break;
        default:
            output = "请输入以上要计算的面积和体积相对应的编号,在(1-5)之间!";

        }
    }

    public static void Guess() {
        int count = 0;
        int nnm = (int) (10 + 90 * Math.random());
        for (int ii = 1; ii <= 100; ii++) {
            count++;
            String numberi = JOptionPane.showInputDialog("请输入100以内的一个整数 !");
            int mi = Integer.parseInt(numberi);
            if (mi <= 0 || mi >= 100) {
                output = "Please an integer 10-99 !";
                JOptionPane.showMessageDialog(null, output);
                continue;
            } else if (mi < nnm) {
                output = "输入的值小于实际值!";
                JOptionPane.showMessageDialog(null, output);
                continue;
            } else if (mi > nnm) {
                output = "输入的值大于实际值!";
                JOptionPane.showMessageDialog(null, output);
                continue;
            } else {
                output = "恭喜你答对了!您最准得分为:" + (110 - count * 10);
                JOptionPane.showMessageDialog(null, output);
                break;
            }
        }
    }

    public static void Triangle() {
        double arr[] = new double[3];

        for (int i = 1; i <= 3; i++) {
            String numa = JOptionPane.showInputDialog("请输入三角形的第" + i + "条边!");
            double v = Double.parseDouble(numa);
            arr[i - 1] = v;
        }
        if (arr[0] == 0 || arr[1] == 0 || arr[2] == 0) {
            output = "您输入的数据错误," + "\n" + "三角形的边长不能为零," + "\n" + "请输入正确的数据 !";
        } else if (arr[0] + arr[1] <= arr[2] || arr[0] + arr[2] <= arr[1]
                || arr[1] + arr[2] <= arr[0])
            output = "您输入的数据必须是任意两边之和要大于第三遍," + "\n" + "请正确输入数据 !";
        else {
            double area = 0;
            double P = 0;
            P = (arr[0] + arr[1] + arr[2]) / 2;
            area = Math.pow(P * (P - arr[0]) * (P - arr[1]) * (P - arr[2]),
                    1.0 / 2);
            output = "三角形的面积为: " + area;
        }
    }

    public static void Rectangle() {
        int[] Arr1 = new int[2];
        for (int i = 1; i <= 2; i++) {
            String y = (i == 1) ? "宽" : "长";
            String num2 = JOptionPane.showInputDialog("请输入要计算的矩形的" + y);
            Arr1[i - 1] = Integer.parseInt(num2);
        }
        if (Arr1[0] == 0 || Arr1[1] == 0)
            output = "矩形的边长输入错误,请正确输入!";
        else {
            int tem = Arr1[0] * Arr1[1];
            output = "矩形的面积为:" + tem;
        }
    }

    public static void Trapizoid() {
        String numc = JOptionPane.showInputDialog("请输入梯形的底边长!");
        String numd = JOptionPane.showInputDialog("请输入梯形的上边长!");
        String nume = JOptionPane.showInputDialog("请输入梯形的高!");
        int A = Integer.parseInt(numc);
        int B = Integer.parseInt(numd);
        int H = Integer.parseInt(nume);
        if (A == 0 || B == 0 || H == 0)
            output = "请正确输入梯形的底边长,上边长以及高!";
        else {
            int tem1 = ((A + B) * H) / 2;
            output = "梯形的面积为:" + tem1;
        }
    }

    public static void Circle() {
        String num4 = JOptionPane.showInputDialog("请输入圆的半径!");
        int radius = Integer.parseInt(num4);
        if (radius == 0)
            output = "请正确输入圆的半径!";
        else {
            double tem2 = PI * Math.pow(radius, 2);
            output = "圆的面积为:" + tem2;
        }
    }

    public static void Globe() {
        String numg = JOptionPane.showInputDialog("请输入球的半径!");
        int radiu = Integer.parseInt(numg);
        if (radiu == 0)
            output = "请正确输入球的半径!";
        else {
            double CircleOfArea = 4 * PI * radiu * radiu;
            double CicleOfVomlue = (4.0 / 3) * PI * radiu * radiu * radiu;
            output = "球的表面积为:" + CircleOfArea + "\n" + "球的体积为:" + CicleOfVomlue;
        }
    }

    public static void Cylinderr() {
        String numberr = JOptionPane.showInputDialog("请输入圆柱体的底面半径 !");
        String number1 = JOptionPane.showInputDialog("请输入圆柱体的高!");
        int a = Integer.parseInt(numberr);
        int x = Integer.parseInt(number1);
        if (a == 0 || x == 0)
            output = "请正确输入圆柱体的底面半径和高!";
        else {
            output = "圆柱体的表面积为: " + Area(a, x) + "\n" + "圆柱体的体积为: "

            + Cylinder(a, x);
        }
    }

    public static double Cylinder(int circle, int height) { // 计算case 6里面圆柱体的体积;
        final double PI = 3.14159;
        double volume = PI * Math.pow(circle, 2) * height;
        return volume;
    }

    public static double Area(int i, int k) { // 计算case 6里面圆柱体的表面积;
        final double PI = 3.14159;
        double surfaceArea = 2.0 * PI * i * k;
        return surfaceArea;
    }
}

不要让肮脏的记忆,迷失了原本纯洁的心灵!
2010-05-13 15:15
快速回复:初学时编的一个小程序,请帮忙简化一下!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044735 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved