如何让JOptionPane计算面积?

时间:2016-10-12 15:40:39

标签: 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 个答案:

答案 0 :(得分:2)

代码的问题在于,每次将输入解析为字符串时,您总是使用相同的字符串值。每次调用函数时,您都会将所有1,2或3用于参数调整到区域函数调用中。所以你需要改变Integer.parseInt()来包含你从用户那里得到的新字符串,如下所示:

// Pass the item list and the desired category as arguments:
var findItem = function(items, category, desiredItem) {
    // apply filter to items, so only those of the given category remain:
    items = items.filter( item => item.category == category );
    // rest of code remains the same:
    var possible = items.some( ({item, probability}) => 
          item === desiredItem && probability > 0 );
    if (!possible) {
        console.log('There is no chance you\'ll ever find a ' + desiredItem);
        return;
    }
    var sum = items.reduce( (sum, {item, probability}) => sum+probability, 0 );
    var t = 10;
    while (true) {
        var value = Math.random() * sum;
        var lootedItem = items.find( 
                ({item, probability}) => (value -= probability) <= 0 ).item;
        if (lootedItem === desiredItem) break; // fixed this condition!
        console.log("Dang! A " + lootedItem + " was found...");
        t--; if (t <= 0) throw "loop";
    }
    console.log("Lucky! A " + desiredItem + " was found!");
}

// Define items here with their category
var items = [
    { item: "rusty nail", probability: 0.25, category:  2 },
    { item: "stone",      probability: 0.23, category:  2 },
    { item: "banana",     probability: 0.20, category:  2 },
    { item: "leaf",       probability: 0.17, category:  5 },
    { item: "mushroom",   probability: 0.10, category:  5 },
    { item: "diamond",    probability: 0.05, category: 10 }
];

// Call function with extra arguments:
findItem(items, 5, 'mushroom');

console.log('second run:');
// This will obviously give a hit immediately, as there is only one possible item:
findItem(items, 10, 'diamond');