简体   繁体   English

累计值-java-简单问题

[英]Accumulated value -java - simple question

one simple question. 一个简单的问题。 I need to sum a value, when a condition is verified, so i have this code 验证条件后,我需要求和一个值,所以我有这段代码

private int sum;
private int sum1;
private int sum2;
private int sum3;

public int pontuationOfplayers() {
    if (winner() == 0) {
        return sum += 20;
    }
    else if (winner()==1) {
        return sum1 += 20;
    }
    else if (winner() ==2) {
        return sum2 += 20;
}
    else
        return sum3 += 20;
    }

the problem that i have is when the method is called it always start again with 0, so the result must be like, 20, 40, 60, but because of new initialization is always 20. 我遇到的问题是,当调用该方法时,它总是从0重新开始,所以结果必须是20、40、60,但是由于新的初始化总是20。

how can i solve that? 我该如何解决? like store the value of the variable 喜欢存储变量的值

thanks! 谢谢!

script:edit 脚本:编辑

You probably want to make sum a class member , eg: 您可能想将sum作为类成员 ,例如:

private int sum = 0;

public int pontuationOfplayers() {
    if (winner() == 0) {
        System.out.println("aqui");
        int value0 = 0;
        return sum += value0+=20;
    }

    else {
        System.out.println("aquiii");
        int value3 = 0;
        return sum += value3+=20;
    }
}

Based on your update I would suggest doing: 根据您的更新,我建议您执行以下操作:

private int sum[] = new int[4];

public int pontuationOfplayers() {
    // (assuming winner() returns 3 for the final case)
    return sum[winner()] += 20;
}

sum should be a member of the class and thus defined sum应该是该类的成员,因此已定义

private int sum = 0;

within the class(but outside the methods). 在类内(但在方法外)。 It is then preserved within the instance of the class, exists for the lifetime of that instance and can be modified by the methods within the class. 然后将其保留在该类的实例内,在该实例的生命周期内存在,并可以通过该类内的方法进行修改。

What you currently have defined exists only for the lifetime of the method invocation, and will go out of scope as soon as you exit that method. 当前定义的内容仅在方法调用的生存期内存在,并且在退出该方法后将超出范围。

make sum an instance variable of your class. 使sum成为您类的实例变量。 currently it is a local variable and hence it is initiatilized per method call. 当前它是一个局部变量,因此每个方法调用都会对其进行初始化。

The sum value will need to be created and stored outside this method if it is called several times and you want to maintain the sum. 如果多次调用总和值,并且您希望保持总和,则需要创建该总和并将其存储在此方法之外。 Either store it in a class variable or a local variable in the method that is calling the pontuationOfplayers() method. 可以将其存储在调用pontuationOfplayers()方法的方法的类变量或局部变量中。

Although other answers solve your problem, they don't address what caused it: you are just starting to learn to program. 尽管其他答案可以解决您的问题,但它们并不能解决造成问题的原因:您只是开始学习编程。

Read beginner tutorials on Java, Java classes and Java OOP (object-oriented programming). 阅读有关Java,Java类和Java OOP(面向对象编程)的初学者教程。 For example, you may start with this question: https://stackoverflow.com/questions/1168919/java-tutorials-for-beginning-programmer 例如,您可以从以下问题开始: https : //stackoverflow.com/questions/1168919/java-tutorials-for-beginning-programmer

Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM