简体   繁体   English

Java:高分方法

[英]Java: Highscore method

I have a program and want to create a simple highscore method for it. 我有一个程序,想要为其创建一个简单的高分方法。 The method will only tell if the current amount of points is higher than the ones before. 该方法只会告诉您当前的点数是否高于以前的点数。

public class Highscore {    
public static int Highscore(int poang) {

    int count = 0;
    int poäng1 = 0;
    int poäng2 = 0;

As you see in above, the counter is set to 0; 如上所示,计数器设置为0;计数器设置为0; . This is to save the first entry. 这是为了保存第一个条目。 However, it resets to 0 every time the method is being used. 但是,每次使用该方法时,它将重置为0。 How can i recode this? 我该如何重新编码? Here is the rest of the code: 这是其余的代码:

    if (count == 0) {
        poäng1 = poang;
        count++;
    } else if (count > 0) {         
            if (poäng2 > poäng1) {              
                poäng1 = poäng2;
            }
    }
    return poäng1; 

    }   
}

Local variables are redefined (and re-initialized) every time the block (in your case - a static method) is entered. 每次输入块(在您的情况下为静态方法)时,都会重新定义(并重新初始化)局部变量。 If you want them to keep their value beyond the scope of that block, they should be defined outside of it. 如果希望它们的值超出该块的范围,则应在它们之外定义它们。

In this case, you could have count as a (static) member: 在这种情况下,您可以count一个(静态)成员:

private static int count = 0;
public static int Highscore(int poang) {
    // Code comes here

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

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