有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在其他静态方法中调用局部变量?

我应该编写一个程序,在用户给定的约束条件之间选择一个随机数,并要求用户输入关于这个数的猜测。该程序会向用户反馈该数字是否高于或低于用户的猜测。记录猜测次数、游戏次数、所有游戏中使用的总猜测次数,以及一个游戏中使用的最低猜测次数

这些结果已打印出来。负责运行游戏的函数(playGame())和负责打印这些结果的函数(getGameResults())必须采用两种不同的方法

我的问题是,我不确定如何将在playGame()方法到getGameResults()方法的整个过程中修改的局部变量

getGameResults()将在另一个方法continuePlayTest()中调用,该方法测试用户的输入,以确定他们是否希望继续玩游戏,因此我认为调用getGameResults()不会起作用,否则此测试也不会起作用。除非我在playGame()中调用continuePlayTest(),但continuePlayTest()在其代码中调用playGame(),这样会使事情复杂化

我们只能使用我们学到的概念。我们不能在前面使用任何概念。 到目前为止,我们已经学习了如何使用静态方法,用于循环、while循环、if/else语句和变量。全局变量的样式不好,因此无法使用

代码:

public class Guess {
public static int MAXIMUM = 100;

public static void main(String[] args) {
    boolean whileTest = false;
    gameIntroduction();
    Scanner console = new Scanner(System.in);
    playGame(console);
}

// Prints the instructions for the game.
public static void gameIntroduction() {
    System.out.println("This process allows you to play a guessing game.");
    System.out.println("I will think of a number between 1 and");
    System.out.println(MAXIMUM + " and will allow you to guess until");
    System.out.println("you get it. For each guess, I will tell you");
    System.out.println("whether the right answer is higher or lower");
    System.out.println("than your guess.");
    System.out.println();       
}

//Takes the user's input and compares it to a randomly selected number. 
public static void playGame(Scanner console) {
    int guesses = 0;
    boolean playTest = false;
    boolean gameTest = false;
    int lastGameGuesses = guesses;
    int numberGuess = 0;
    int totalGuesses = 0;
    int bestGame = 0;
    int games = 0;
    guesses = 0;
    games++;
    System.out.println("I'm thinking of  a number between 1 and " + MAXIMUM + "...");
    Random number = new Random();
    int randomNumber = number.nextInt(MAXIMUM) + 1;
    while (!(gameTest)){
        System.out.print("Your guess? ");
        numberGuess = console.nextInt();
        guesses++;
        if (randomNumber < numberGuess){
            System.out.println("It's lower.");
        } else if (randomNumber > numberGuess){
                System.out.println("It's higher.");
            } else {
        gameTest = true;
        }
        bestGame = guesses;
        if (guesses < lastGameGuesses) {
            bestGame = guesses;
        }
    }
    System.out.println("You got it right in " + guesses + " guesses");
    totalGuesses += guesses;
    continueTest(playTest, console, games, totalGuesses, guesses, bestGame);
}


public static void continueTest(boolean test, Scanner console, int games, int totalGuesses, int guesses, int bestGame) {
    while (!(test)){
        System.out.print("Do you want to play again? ");
        String inputTest = (console.next()).toUpperCase();
        if (inputTest.contains("Y")){
            playGame(console);
        } else if (inputTest.contains("N")){
            test = true;
            }
        }
    getGameResults(games, totalGuesses, guesses, bestGame);
    }       

// Prints the results of the game, in terms of the total number
// of games, total guesses, average guesses per game and best game.
public static void getGameResults(int games, int totalGuesses, int guesses, int bestGame) {
    System.out.println("Overall results:");
    System.out.println("\ttotal games   = " + games);
    System.out.println("\ttotal guesses = " + totalGuesses);
    System.out.println("\tguesses/games = " + ((double)Math.round(guesses/games) * 100)/100);
    System.out.println("\tbest game     = " + bestGame);
}   

}


共 (0) 个答案