Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中不断添加值(int)?_Java_Variables_Int - Fatal编程技术网

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

如何在java中不断添加值(int)?,java,variables,int,Java,Variables,Int,我很难理解,也可能记不起您是如何不断添加到字段/变量的。我希望它记住我最后的输入,并将其添加到int值的总和中。所以每次我输入一个新的数字,总数就会不断增加 我还有另外两个类,只包含Getter/Setter和Construtor public class foo { static Scanner scan = new Scanner(System.in); private static int input; static A a = new A(10); sta

我很难理解,也可能记不起您是如何不断添加到字段/变量的。我希望它记住我最后的输入,并将其添加到int值的总和中。所以每次我输入一个新的数字,总数就会不断增加

我还有另外两个类,只包含Getter/Setter和Construtor

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())

不要在(1!=2)时使用像
这样的空洞语句。改用
while(true)