【问题标题】:How to keep adding to your values (int) in java?如何在java中不断增加你的值(int)?
【发布时间】:2019-06-25 08:17:06
【问题描述】:

我无法理解并且可能无法记住您是如何不断添加到字段/变量中的。我希望它记住我的最后输入并将其添加到 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);
    }
}

【问题讨论】:

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

标签: java variables int


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多