当前位置: 动力学知识库 > 问答 > 编程问答 >

Adding up, multiplying and finding the number with the lowest value in JAVA

问题描述:

Write into a TextField 10 whole numbers like this: 1, 2, 3, .... After you click the + button (which you create) it will do the sum of the numbers in a jLabel, then there is a * button which will multiply all the numbers and print them out in a seperate jLabel, and then there's a Minimum button, which will show the number in the array with the lowest value in yet another seperate jLabel.

Yes, I'm sorry, I didn't make myself clear. I only need to know what is the code for the last part, the MINIMUM part. I tried java.lang.Math_MIN (dunno if it's written right) and I tried the length operator, none of them worked...

Note: It's done in NetBeans, and I'm not from an english speaking country so if i send you the code parts of it are in my language.

网友答案:

This is for the sum, the rest should be easy.

btnPlus = new JButton("+");
        btnPlus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String nums = textField.getText();

                if (nums.matches("[0-9]+") && nums.length() == 10) {

                    char[] charArray = nums.toCharArray();
                    int sum = 0;

                    for (int i = 0; i < charArray.length; i++) {
                        sum += Character.getNumericValue(charArray[i]);
                    }
                    label_1.setText(Integer.toString(sum));
                }
            }
分享给朋友:
您可能感兴趣的文章:
随机阅读: