在java

时间:2017-05-16 17:37:38

标签: java netbeans

我是java的初学者,这是我第一次在netbeans IDE中使用编程语言java。

我已经达到了这一点,我将我的应用程序用于制作顶级王牌:

![1]:https://i.stack.imgur.com/b77C2.png

然而,计数器Jlabel命名的可用点数低于0到负数,我不想要,其他一切都很好!

以下是我的计数器代码:

public class addingPointsUI extends javax.swing.JFrame {
void availablePoints() {
    if(Integer.parseInt(availablePoints.getText()) >=0 && Integer.parseInt(availablePoints.getText()) <=50)
    {
    System.out.println("...");
    }
else{
     JOptionPane.showMessageDialog (null,"You have no more available points");
         }
}
int pointsAvailable=50;
    int speed=0;
    int power=0;
    int focus=0;
    int agility=0;
    int defence=0;
    int attack=0;

public addingPointsUI() {
    initComponents();
}

以下是speed plus按钮的代码:

if (speed < 10 ){

        speed = speed + 1 ;

        pointsAvailable = pointsAvailable - 1 ;

        availablePoints.setText(String.valueOf(pointsAvailable));

        speedPoints.setText(String.valueOf(speed));

         } else{

         JOptionPane.showMessageDialog (null, "You have maxed out your Speed");
    }
}            

以下是速度减去按钮的代码:

if (speed > 0 ){

        speed = speed - 1;

        pointsAvailable = pointsAvailable +1;

        availablePoints.setText(String.valueOf(pointsAvailable));

        speedPoints.setText(String.valueOf(speed));

    }else {

            JOptionPane.showMessageDialog(null,"You cannot take anymore points from Speed");
    }
}                                          

请注意,我是使用java的初学者,我也是本网站的新手。

感谢您的回复。

1 个答案:

答案 0 :(得分:1)

在允许用户添加更多积分之前,您可以检查以确保有可用积分。

if (speed < 10 ) {
    if (pointAvaialable <= 0) {
        JOptionPane.showMessageDialog(null, "You are out of available points");
        return;
    }

    speed = speed + 1 ;
    pointsAvailable = pointsAvailable - 1 ;
    availablePoints.setText(String.valueOf(pointsAvailable));
    speedPoints.setText(String.valueOf(speed));
} else {
     JOptionPane.showMessageDialog (null, "You have maxed out your Speed");
}