涉及方法未定义类型的复杂问题

时间:2018-12-28 04:08:38

标签: java algorithm

我目前正在编写此程序,您会在其中猜测计算机随机生成的数字。但是,我尝试在VS代码中运行时不断得到的结果是:对于类型extracted(input),方法RandomNumber是未定义的。我已经搜索并注意到Google和其他平台上的类似问题,但我似乎听不懂他们在说什么。因此,如果有人会这么友好并向我解释问题所在,将不胜感激。我也想知道这是否可能是软件错误,因为我的一个朋友能够在Eclipse代码中完美地在Eclipse中运行此代码,而我却无法在VS代码中。

代码:

import java.util.Scanner;

public class RandomNumber
{
    public static void main(String [] args )
    {
        int i = (int)( Math.random() * 101);
        int NumberToGuess = i;
        int NumberGuessed;
        int NumberOfTries;
        Scanner input = new Scanner(System.in);
        int GuessedNumber;
        boolean win = false;

        while (win == false)
        {
            System.out.print( "Take a Guess Between Number 1 ~ 100! ");
            GuessedNumber = extracted(input);
            NumberOfTries++;

            if ( NumberGuessed < NumberToGuess );
            {
                System.out.print( "Your Guessed Number is Too Low!" );
            }
            if ( NumberGuessed > NumberToGuess )
            {
                System.out.print( " Your Guessed Number is Too High!");
            }
            else if ( GuessedNumber == NumberToGuess )
            {
                win  = true;
            }

        }

        System.out.print( "Your Guessed Number is Correct! You Win!");
        System.out.print( "The Number To Guess Was:" + NumberToGuess );
        System.out.print( "The Total Number Of Tries You Took is:" + NumberOfTries );
    }
}

3 个答案:

答案 0 :(得分:0)

您正在调用一种方法:extracted()并传入input,并使用该方法的结果来设置GuessedNumber。除了extracted可以做什么,哪个还可以?我不知道,编译器也不知道。如果您从示例中借用了此代码,请检查并查看它们是否实现了一个名为such的方法,并将该方法也添加到您的类中。

我看到的另一个潜在问题是GuessedNumberNumberGuessed可以互换使用,这几乎肯定不是您想要发生的事情。

答案 1 :(得分:0)

让我们修改您的代码,使其正确无误:

public static void main(String[] args) {
    int i = (int)( Math.random() * 101);
    int NumberToGuess = i;
    System.out.println("rand is " + NumberToGuess);
    int NumberOfTries = 0;
    Scanner input = new Scanner(System.in);
    int GuessedNumber;
    boolean win = false;

    while (win == false)
    {
        System.out.print( "Take a Guess Between Number 1 ~ 100! ");
        GuessedNumber = input.nextInt();
        NumberOfTries++;

        if ( GuessedNumber < NumberToGuess )
        {
            System.out.print( "Your Guessed Number is Too Low!" );
        }
        else if ( GuessedNumber > NumberToGuess )
        {
            System.out.print( " Your Guessed Number is Too High!");
        }
        else 
        {
            win  = true;
        }

    }

    System.out.print( "Your Guessed Number is Correct! You Win!");
    System.out.print( "The Number To Guess Was:" + NumberToGuess );
    System.out.print( "The Total Number Of Tries You Took is:" + NumberOfTries );

}

首先,删除参数NumberGuessed,这很奇怪。

然后,我们需要读取用户的猜测int编号:GuessedNumber = input.nextInt();

之后,我们需要将尝试计数器初始化为0:int NumberOfTries = 0;

最后,我们需要更正ifelse ifelse语句:

if ( GuessedNumber < NumberToGuess )
{
    System.out.print( "Your Guessed Number is Too Low!" );
}
else if ( GuessedNumber > NumberToGuess )
{
    System.out.print( " Your Guessed Number is Too High!");
}
else 
{
    win  = true;
}

答案 2 :(得分:0)

请遵循Java变量命名约定。变量名称应以小写字母开头。接下来,将while (!condition)替换为while (condition == false),将ThreadLocalRandom替换为Math.random()(例如,您的当前生成器可能会生成0)。通过测试在分配给else中当前guess是否相等,我们可以完全消除原始win。您的格式(输出时)为 odd 。将所有内容放在一起,您的代码可能看起来像

int numberToGuess = 1 + ThreadLocalRandom.current().nextInt(100);
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
boolean win = false;

while (!win) {
    System.out.print("Take a Guess Between Number 1 ~ 100! ");
    int guess = input.nextInt();
    numberOfTries++;
    win = (guess == numberToGuess);
    if (guess < numberToGuess) {
        System.out.println("Your Guess is Too Low!");
    } else if (guess > numberToGuess) {
        System.out.println("Your Guess is Too High!");
    }
}

System.out.println("Correct! You Win!");
System.out.printf("The Number To Guess Was: %d%n", numberToGuess);
System.out.printf("The Total Number Of Tries You Took is: %d%n", numberOfTries);