【问题标题】:Why is an integer resetting to 100 when it should update to something different为什么当整数应该更新为不同的值时它会重置为 100
【发布时间】:2013-09-29 23:03:38
【问题描述】:

只要循环运行,下面的代码墙应该会更改“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");
    }
}

【问题讨论】:

  • 我刚刚意识到在“估计”方法中我的输入被错误地命名了。我将它们更改为变量名最低猜测和最高猜测,但没有发生任何不同。
  • 快速评论 - 可能/可能不会导致任何问题,但通常使用与函数名称相同的变量名称是不好的风格。考虑getEstimate()int estimataion
  • 请格式化代码
  • lowestguesstakestabmain 内的lowestguess 相同。仅仅因为两个变量共享相同的名称并不意味着它们相同的。
  • 将 Math.random() 转换为 int 并说您收到相同的值是个坏主意。 Math.random 返回 double 并且如果你转换为 int 你总是得到一个值(大多数时候为 0)。更喜欢使用 Random 类来生成随机值,否则使用 Math.Random 的 double 值并且不要强制转换为 int

标签: java variables loops methods scope


【解决方案1】:

整数是不可变的对象。

lowestguess = estimate;

在这一行(以及highestguess 的行),您正在更改lowestguess 的本地副本,而不是您传递给它的副本。

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

高于你的主要位置

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

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

对于吹毛求疵的人来说:我在这里使用“全局”,因为这里没有 OO。在这种情况下,静态变量在所有意图和目的上都是全局变量。

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2019-11-14
    • 2018-05-07
    • 1970-01-01
    • 2022-08-03
    • 2018-08-28
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多