如何从其他方法获取数据?

时间:2014-11-26 23:41:29

标签: java variables

有人知道如何在增加后转移计数器值吗?所以,如果你正确回答,它会在下一个方法中改为1?

package swag;
import java.util.Scanner;

public class Main {
    public static void enter(){
        System.out.print("welcome to the impossibe game! Press enter to start.");

        Scanner input = new Scanner(System.in);
        String enter = input.nextLine();    

        if (enter.equals("")){
            System.out.println("              Level one");
        }else{
            System.out.println("Please press enter");
        }
    }

    public static void firstlevel(){
        System.out.println("What is the tenth digit of PI?");

        Scanner input = new Scanner(System.in);
        int awnser = input.nextInt();   
        int awnser1 = 5;
        int counter = 0;

        if (awnser == awnser1 ){
            System.out.println("Correct!");

            counter++;
            System.out.println("            Score:   " +counter + "/1");

        }else{
            System.out.println("Wrong!");
            System.out.println("            Score:"+ counter+"/1");
        }   
    }

    public static void secondlevel(){
        System.out.println("a king and queen get on a boat. then the boat sinks. how many people are alive");
        Scanner input = new Scanner(System.in);
        String awnser = input.nextLine();

        if (awnser.equals("two ")){
            System.out.println("      Correct!");
        }
    }

    public static void main(String args[]){
        enter();
        firstlevel();
    }
}

2 个答案:

答案 0 :(得分:0)

啊,你定义计数器的方式,只能在firstLevel中看到。

要做的最好的事情就是把它变成一个类变量'。要做到这一点:

  1. 从firstLevel方法中删除int counter = 0;
  2. static int counter = 0;
  3. 之后的下一行添加public class Main {

    所以你班级的开头应该是这样的:

    public class Main {
        static int counter = 0;
    

    现在所有方法都可以看到计数器。

答案 1 :(得分:0)

我强烈建议不要使用其他人建议的静态计数器。静态可变对象往往违反面向对象编程的原则。如果您将游戏的功能分离为方法,那么您将拥有一个更加美观的主要方法:

public static void main(String args[]) {
    // Lets create a new Game object. it will keep track of the counter itself!
    Game game = new Game();
    // Then we only have to call the methods defined below..
    game.enter();
    game.firstLevel();
    game.secondlevel();
}

现在包含所有逻辑的类Game的代码:

public class Game {
    // Some static final members. they are not meant to change throughout the execution of the program!
    // The advantage of defining INDENTAION just once, is that you can easily change it in one place and it will always be consistent!
    private static final String INDENTAION = "\t\t";
    private static final int TOTAL_POINTS = 2;
    // We can use the same scanner object in each method!
    private Scanner input = new Scanner(System.in);
    // Just like the counter. it will be accessible in each method and refer to the same integer!
    private int counter = 0;

    public void enter() {
        System.out.print("welcome to the impossibe game! Press enter to start.");

        Scanner input = new Scanner(System.in);
        String enter = input.nextLine();

        if (enter.equals("")) {
            System.out.println(INDENTAION + "Level one");
        } else {
            // I am not sure why you put this here, but I'll leave it in for now
            System.out.println("Please press enter");
        }
    }

    // We put that code into seperate methods, since it will get called multiple times!
    private void answerCorrect() {
        System.out.println("Correct!");
        counter++;
        printScore();
    }

    private void answerWrong() {
        System.out.println("Wrong!");
        printScore();
    }

    private void printScore() {
        System.out.println(INDENTAION + "Score: " + counter +"/"+ TOTAL_POINTS);
    }

    public void firstLevel() {
        System.out.println("What is the tenth digit of PI?");
        int awnser = input.nextInt();

        if (awnser == 5) {
            answerCorrect();
        }else{
            answerWrong();
        }
    }

    public void secondlevel() {
        System.out.println("a king and queen get on a boat. then the boat sinks. how many people are alive");
        String awnser = input.nextLine();

        if (awnser.equals("two") || awnser.equals("2")) {
            answerCorrect();
        } else {
            answerWrong();
        }
    }
}