java - 如何在java中不断添加你的值(int)?

标签 java variables int

我无法理解并且可能无法记住您如何不断添加到字段/变量。我希望它记住我最后的输入并将其添加到 int 值的总和中。因此,每次我输入新数字时,总数都会不断增加。

我还有另外 2 个类,其中只包含 Getter/Setter 和构造函数

public class foo {
    static Scanner scan = new Scanner(System.in);
    private static int input;
    static A a = new A(10);
    static B b = new B(10);

    public static void main(String[] args) {
        while (1 != 2) {  
            keepValue();
        }         
    }

    public static void keepValue() {
        a.setI(scan.nextInt());
        int sum = a.getI() + b.getJ();
        System.out.println("the sum of the two numbers is: "+sum);
    }
}

最佳答案

也许a.setI(a.getI() + scan.nextInt())

关于java - 如何在java中不断添加你的值(int)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467886/

相关文章:

java - 将 int[] 添加到动态数组(例如 Array List)

javascript - 在 J2V8 中使用 promise

java - 绕特定点旋转(例如,绕0,0,0旋转)

javascript - JavaScript 中的变量定义

javascript - 如何在javascript中处理变量

arrays - 如何在 Swift 中转换 Int 数组中的字符串(数字)

java - 为 int 值设置限制

java - JPanel 自己的坐标

Java api - 以 _ [下划线] 开头的类名

ios - 为什么克隆变量仍然影响原始对象? swift