Home > Blockchain >  How do I change my level after a certain score has been reached? (Java, NetBeans)
How do I change my level after a certain score has been reached? (Java, NetBeans)

Time:12-28

I already have some part of the code but I'm not sure whether or not it's okay like this. I need to change it somehow because it doesn't work like this. This is my current code;

public void changeLevel(){
        for(int i = 1; i < getScore(); i  ){
            if(getScore() % 100 == 0){
                level  ;
            }
        } 
    }

In this case, every time after 100 points I want my level to add 1. If anybody could help me out, that would be hugely appreciated! :)

CodePudding user response:

Please Consider below example,

public class Person {
    private Integer score;
    private Integer level;

    public void changeLevel(){
        this.level = this.score / 100 > 0 ? this.level   this.score / 100 : this.level;
        this.score = this.score % 100;
    }

    public Person(Integer score, Integer level) {
        this.score = score;
        this.level = level;
    }

    public String toString() {
        return "Person{"  
                "score="   score  
                ", level="   level  
                '}';
    }
}

And you may validate result as,

public class Main {
public static void main(String[] args) {
    Person person = new Person(427, 2);
    person.changeLevel();
    System.out.println(person);
}

}

CodePudding user response:

The level change code should rather be related to the score change code.

Something like :

public class Person {
    private int score = 0;
    private int level = 0;

    public void incrementScore() {
        if (  this.score % 100 == 0)   this.level;
    }

    public static void main(String[] args) {
        Person person = new Person();
        for (int i = 0; i < 200; i  ) person.incrementScore();
        System.out.println("Person score: "   person.score);
        System.out.println("Person level: "   person.level);
    }
}
  •  Tags:  
  • java
  • Related