简体   繁体   English

如何从另一个类的静态方法更改静态变量的值

[英]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.

相关问题 如何从一个方法更改静态类变量的值,然后从另一个方法获取它呢? - How can I change the value of a static class variable from a method, then get it from an other method? 如何通过方法更改类内部的静态变量 - How to change static variable inside in a class by a method 如何从另一个类访问静态变量? - How to access static variable from another class? 将值从一个类的静态变量保存到另一个Java - Save value from a static variable from a class to another java 通过另一个类更改静态变量 - change static variable through another class 从一类访问静态变量到另一类,但获得不正确的值 - Accessing Static variable from one class to another but getting incorrect value 如何从一个方法,另一个类的变量数据中读取公共静态变量并获取更新的数据 - How do I read in a public static, with variable data from a method, from another class and get updated data 如何在另一个类中使用静态类中的变量? - How do I use a variable from a static class in another class? 如何从另一个类的静态类中调用方法? - How to call a method from a static class in another class? 如何使用一个类中的静态变量来更新另一个类中的非静态实例变量? - How to use a static variable from a class to update a non-static instance variable in another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM