[英]how to change the value of a static variable from a static method of another class
hi guys i got this variable in my first class 嗨,大家好,我在第一堂课上得到了这个变量
public static double credit;
in the constructor of the first class i got 在第一堂课的构造函数中,我得到了
credit=0.0;
then i got this listener-code that shows me the value on the graphic window after the user adds the funds 然后我得到了这个侦听器代码,该代码在用户添加资金后在图形窗口上显示了我的价值
Catalogo.addCredit(credit);
creditLabel.setText("Credito: " + getCredit()+"€");
using those 2 static methods, this for the value return ( in the first class) 使用这两个静态方法,用于返回值(在第一类中)
public static double getCredit() {
return credit; }
and for increase the funds i use this one of another class 为了增加资金,我使用了另一类
public static void addCredit(double creditl) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String cred = JOptionPane.showInputDialog(frame, "Inserisci l'importo da aggiungere:");
if (cred == null) {
return; }
if ((cred.matches("[0-9]*\\.[0-9]+")) || (cred.matches("[0-9]+"))) { //checks if the string is a valid double value or an int value
double credits = (double) Double.parseDouble(cred); //conversion into double
if (credits >= 0.0) {
JOptionPane.showMessageDialog(frame, "Credito aggiornato con successo");
creditl = creditl + credits;} }
if (!(cred.matches("[0-9]+")) && !(cred.matches("[0-9]*\\.[0-9]+"))) { //checks if the user didn't insert a correct double or int
JOptionPane.showMessageDialog(frame, "Importo inserito errato");
addCredit(creditl); }
}
now the problem is that the credit variable doesn't increase at all and i don't know why it seems that all is good, or maybe am i missing something ? 现在的问题是信用变量根本没有增加,我也不知道为什么一切似乎都很好,或者我错过了什么? the input goes wel in the mean that it checks for the proper value and then it says that the credit has been updated but it's not and i don't know why this is happening
输入表示满意,这意味着它检查了正确的值,然后说信用已更新,但是没有更新,我不知道为什么会这样
NOTE: i have tried to make this variable a non static var and renaming all methods to non static but it seems that it's the same thing, same problem 注意:我试图使此变量成为非静态var,并将所有方法重命名为非静态,但似乎是同一回事,同样的问题
You're not setting the credit
class variable anywhere in your code. 您没有在代码中的任何地方设置
credit
等级变量。 You have another local variable named creditl
, maybe this is a typo that is part of your problem. 您还有一个名为
creditl
局部变量,也许这是您输入问题的一部分。
Specifically, this line might be the issue: 具体来说,这行可能是问题所在:
creditl = creditl + credits;
(In any case, you should revisit your design.) (无论如何,您都应该重新设计。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.