gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:38:55 24 4
gpt4 key购买 nike

我目前正在编写这段程序,您可以在其中猜测计算机随机生成的数字。但是我在 VS 代码中尝试运行它时经常得到的结果是:extracted(input) 方法对于 RandomNumber 类型是未定义的。我在谷歌和其他平台上搜索并注意到了类似的问题,但我似乎无法理解他们在说什么。因此,如果有人会这么好并向我解释问题是什么,我们将不胜感激。我还想知道这是否可能是软件错误,因为我的一个 friend 能够在 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 );
}
}

最佳答案

让我们编辑您的代码,使其正确无误:

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,它是奇数。

然后,我们需要读取用户猜测的整数: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;
}

关于java - 涉及方法未定义类型的复杂问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com