多个方法的全局Int变量方法



我必须建立一个数学程序,根据用户的年级水平向他们提供随机问题,如果他们做得好,就会提高难度。我在这里设置了这个代码,它可以运行问题,如果它们满足要求,它会显示较难的问题,但是,如果我用"I"来确定成员问题是如何完成的,如果分离程序将运行较难的问题,那么返回并完成较容易的问题

因此,基本上,我已经尝试为全局"I"编写一个所有其他方法都会使用的方法,然而,当我用该方法替换"I"时,它停止计数,并继续无限显示问题,我不知道如何解决这个问题。

import java.util.Scanner; 
import java.util.*;
import java.util.Date; 
public class Quiz {
    public static void main(String[] args) {
        int answer;
        int correct;
        double current_score = 100.00;
        //      int i = 0;
        while (questionsDone() < 10) { // start of question loop
            int random = (int) (Math.random() * 9 + 0);
            int random2 = (int) (Math.random() * 9 + 0);
            System.out.print("What is the sum of" + " ");
            System.out.print(random);
            System.out.print(" + " + random2 + " ");
            System.out.print("=" + " ");
            Scanner scan = new Scanner(System.in);
            //answer
            answer = scan.nextInt();
            correct = random + random2;
            if (answer == correct) { // start of result display
                System.out.println("You are correct");
            } else if (answer != correct) {
                System.out.println("That wasn't right");
                current_score = (current_score - 10.00);
            }
            System.out.println("Your current percentage is " + current_score); // end of result display
            // i++; // raise number of questions given by 1
            if (questionsDone() == 5 && current_score >= 75) { // code to move up or down year level
                System.out.println("You are doing well! Let's increase the difficulty a little");
                Year1_10Questions();
            }
        }
    }
    public static void Year1_10Questions() {
        int i = 0;
        int answer;
        int correct;
        double current_score = 100.00;
        while (i < 10) { // start of question loop
            int random = (int) (Math.random() * 9 + 0);
            int random2 = (int) (Math.random() * 9 + 0);
            int random3 = (int) (Math.random() * 2 + 1);
            String operator = "";
            switch (random3) {
                case 1:
                    operator = "+";
                    break;
                case 2:
                    operator = "-";
                    break;
            }
            System.out.print("What is the sum of ");
            System.out.print(" " + random + " ");
            System.out.print(operator + " ");
            System.out.print(random2 + " ");
            Scanner scan = new Scanner(System.in);
            //answer
            answer = scan.nextInt();
            if (random3 == 1) {
                correct = random + random2;
                if (answer == correct) { // start of result display
                    System.out.println("You are correct");
                } else if (answer != correct) {
                    System.out.println("That wasn't right");
                    current_score = (current_score - 10);
                }
            } else if (random3 == 2) {
                correct = random - random2;
                if (answer == correct) { // start of result display
                    System.out.println("You are correct");
                } else if (answer != correct) {
                    System.out.println("That wasn't right");
                    current_score = (current_score - 10);
                }
            }
            System.out.println("Your current percentage is " + current_score); // end of result display
            i++; // raise number of questions given by 1
        }
    } // end of year 1_10 questions
    public static int questionsDone() {
        int i = 0;
        i++;
        return i;
    }
}

由于所有方法都在同一个类中,因此可以在类级别上定义i

public class Quiz {
    static int i;
    ...
}

相关内容

  • 没有找到相关文章