我是Java新手,在使用方法时遇到麻烦。我正在尝试显示确定方法AndreDisplay,但是我尝试过的方法不起作用。当我把东西放在main中的defineAndDisplay()的括号中时,我得到一个错误。我不确定自己出了什么问题,任何指导都将对您有所帮助,谢谢!
public static void determineAndDisplay(Scanner userInput, int pNum)
{
//variables
int count = 0, high = 0, low = 0;
//loop
while(true){
System.out.print("Enter an integer, or -99 to quit: --> ");
pNum = userInput.nextInt();
if(pNum == -99)
{
break;
} //if end
count++;
if(count == 1)
{
high = pNum;
low = pNum;
} //if end
//highest
if(pNum > high)
{
high = pNum;
} //if end
//smallest
if(pNum < low)
{
low = pNum;
} //if end
} //while end
if (count == 0)
{
System.out.println("\nYou did not enter any numbers.");
} //if end
else
{
System.out.println("\nLargest integer entered: " + high);
System.out.println("Smallest integer entered: " + low);
} //else end
} // determineandDisplay end
public static void main(String[] args) {
// start code here
Scanner goGet = new Scanner(System.in);
String doAgain = "Yes";
while (doAgain.equalsIgnoreCase("YES")) {
// call method
determineAndDisplay();
// repeat
System.out.print("\nEnter yes to repeat --> ");
doAgain = goGet.next();
} //end while loop
} // main end
public class MainClass{
public static void determineAndDisplay(Scanner userInput, int pNum){
// your code
}
public static void main(String[] args){
// your code
}
}
此外,请确保您的main方法始终位于与代码文件同名的公共类中。另外,请确保已导入扫描仪。