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

标签 java methods global-variables

我必须建立一个数学程序,根据用户的年级水平向他们提供随机问题,如果他们做得好,就提高难度。我在这里设置了运行问题的代码,如果它们满足要求,它会显示更难的问题,但是如果我用“i”来确定他们如何完成成员(member)问题,如果分开,程序将运行更难的问题然后回去完成更简单的问题

所以基本上我尝试为全局“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;
    ...
}

关于java - 多个方法的全局Int变量方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25831314/

相关文章:

java - C3P0 一台服务器多个数据库

java - 如果您不在 java 8 rcp 应用程序中指定 maxmetaspace 参数,会发生什么情况?

javascript - Node.js 全局要求

java - 无法运行我的类(class)

java - 无法从读取标准输入流的方法返回 main()

javascript - 术语 "global property"和 "global variable"是同义词吗?

JavaScript(变量范围): variable does not get the value assigned to it

java - Maven Jdepend 报告不包含任何数据

java - 如何在 Spring Web Flow 2 中的 View 状态和操作状态之间传递模型数据

javascript - 这行Js代码到底是做什么的呢?