每当循环运行时,下面的代码墙均应更改“ lowestguess”和“ highestguess”的值。理想情况下,它的意思是将最高的猜测值从100降低,将最低的猜测值从1提高,以使程序无法猜测比已经猜测的值低或高的数字(减少完成程序所需的循环次数。

但是,每次运行“ takestab”功能时,最低猜测和最高猜测总是分别重置为默认值1和100。不知道这是怎么回事,因为我没有收到任何错误。

是否可能因为这里的方法是私有的,所以它们不会在各自范围之外更新变量?

import java.util.Scanner;

public class AIGuessing
{
    public static void main(String[]args)
    {
        //DECLARATIONS*
        Scanner inputReader = new Scanner(System.in);
        int number;
        int guess;
        int guesses = 0;
        int lowestguess = 1;
        int highestguess = 100;

        //code
        System.out.println("Welcome!");
        System.out.println("Please enter a number for the AI to guess");
        number = inputReader.nextInt();

        //computer tries to guess number
        do
        {
            guesses++;
            guess = takestab(lowestguess, highestguess, number);
            System.out.println("\"I guess " +guess+ ".\"");
        }while(guess != number);

        if(guess == number)
        {
            System.out.println("WOO! I got it!");
        }

        if(guesses >= 1 && guesses ==2)
        {
            System.out.println(guesses + " guesses? I..AM...GOD!!!!");
        }
        else if(guesses >= 3 && guesses <= 5)
        {
            System.out.println(guesses + " guesses? Hooray! I'm as average as gravy!!!");
        }
        else if(guesses >= 6 && guesses <= 8)
        {
            System.out.println(guesses + " guesses? I only guess when I'm drunk");
        }
        else if(guesses >= 9)
        {
            System.out.println(guesses + " guesses? Bugger me... turn me into scrap");
        }
        //end code
    }

    public static int takestab(Integer lowestguess, Integer highestguess, Integer number)
    {

        int estimate;
        estimate = estimate(lowestguess, highestguess);
        System.out.println("Estimate is: "+estimate+".");

        if(estimate < number && estimate > lowestguess)
        {
            lowestguess = estimate;
            barkchange(lowestguess, highestguess, estimate);
        }
        else if(estimate > number && estimate < highestguess)
        {
            highestguess = estimate;
            barkchange(lowestguess, highestguess, estimate);
        }
                return estimate;
    }

    //function to generate and return a random number
    public static int estimate(int low, int high)
    {
        int comGuess;
        comGuess = (low + (int)(Math.random() * ((high - low) + low)));
        return comGuess;
    }

    //function to 'bark' the changes of lowest and highest guesses
    public static void barkchange(Integer lowestguess, Integer highestguess, Integer guess)
    {
        System.out.println("Current guess is: "+guess+".");
        System.out.println("Lowest guess is: "+lowestguess+".");
        System.out.println("Highest guess is: "+highestguess+".\n");
    }
}

最佳答案

整数是一个不变的对象。

lowestguess = estimate;


在此行(以及highestguess的行),您要更改的最低猜测的本地副本,而不是您传递给它的副本。

在这种情况下,最简单的解决方案是“全局”(静态)声明lowestguess和highestguess。

在您的主要看跌期权之上

static int highestGuess = 100;
static int lowestGuess = 1;


然后,您可以将参数删除到方法中,而不必再传递它们。您可以直接引用静态的。

除了nitpickers:我在这里使用“全局”是因为这里没有OO。在所有情况下,静态变量在所有情况下都是全局变量。

08-08 03:14