如何创建每10个正确问题都会提高一个水平的FOR

时间:2019-05-27 16:37:50

标签: java android

我必须找到每10个正确的问题提出1个级别的方法,依此类推。

void updateScoreAndLevel(int answerGiven){

    if(isCorrect(answerGiven)) {
        for (int i = 1; i <= currentLevel; i = i + 100) {
            currentScore = currentScore + 100;
        }
        for (int i = 10; i <= currentScore; i++) {
            currentLevel = currentLevel + i;
        }

    }else{
        currentScore = currentScore - 50;
        currentLevel = currentLevel ;
    }

我希望每10个正确的问题上一个级别,但不会给我

2 个答案:

答案 0 :(得分:0)

        if(isCorrect) {
        for (int i = 1; i <= currentLevel; i = i + 100) {
            currentScore = currentScore + 100;
            amountCorrect + 1;
        }
        for (int i = 10; i <= currentScore; i++) {
            currentLevel = currentLevel + i;
        }

    }else{
        currentScore = currentScore - 50;
        currentLevel = currentLevel ;
    }

    if(amountCorrect % 10 == 1){
        currentLevel + 1;
    }

我想是这样的。您可以添加amountCorrect整数。玩家每次做对时哪个上升。如果他弄错了,可以降低它(如果需要)

%,又称模(对名字不好),将检查玩家是否有10个或更多正确答案并添加等级。这就是你想要的吗?

答案 1 :(得分:0)

这是我在评论中表示的一个更长的例子(很抱歉,如果我的语法已关闭,但是我已经好几年没有写Java了):

// Defines global variables (including parallel arrays of questions/answers)
String[] questions = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
String[] answers = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
int score = 0;
int level = 1;
int counter = 0;

// Loops through all questions
for(int i = 0; i < questions.length; i++){
  String nextQuestion = questions[i];

  // (You should show nextQuestion to the user and get their answer here...)
  // (Eg: https://www.mkyong.com/android/android-prompt-user-input-dialog-example/)
  String answer = answers[i]; // This demo assumes that the user answers correctly

  // Checks if answer is correct, and updates variables accordingly
  if(answer == answers[i]){
    // Correct answer increases score and counter (and sometimes level)
    score = score + 100;
    counter = counter + 1;
    // After 3 correct answers, changes level and resets counter
    if(counter == 3){
      level = level + 1;
      counter = 0;
    }
  }
  else{
    // Incorrect answer decreases score
    score = score - 50;
  }
}