java - 为什么应该更新为不同值的整数却重置为 100

标签 java variables loops methods scope

下面的代码墙应该在循环运行时随时更改“lowestguess”和“highestguess”的值。理想情况下,它意味着将最高猜测值从 100 降低,并将最低猜测值从 1 提高,以便程序无法猜测比已经猜测的数字更低或更高的数字(减少完成程序所需的循环量。

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

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

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的本地副本,而不是您传递给它的副本。

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

在你的主要看跌期权上方

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

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

对吹毛求疵者的补充:我在这里使用了“全局”,因为这里没有面向对象。在这种情况下,静态变量实际上是一个全局变量。

关于java - 为什么应该更新为不同值的整数却重置为 100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18986551/

相关文章:

java-8 - GetPrimitiveArrayCritical OpenJDK 热点从不复制吗?

嵌套 For 循环后的计数值 - 谷歌撰写

c++ - 如何退出嵌套循环

Python:根据条目的位置删除列表中的单个条目

excel - 这条线是 VBA 中不必要的线吗?

Java:为什么这个4个字符的字符串会创建一个包含6个数据的byte[]?

java - 顶点 : handling POST body is so slow

java - 驻留在 Wildfly 模块中的 JPA 实体类不会被扫描

php - 为什么我不能像其他函数一样使用变量来存储 echo() ?

java - 初始化静态变量时使用局部变量