[英]Can't get output for method
I'm new to Java and having trouble with methods.我是 Java 的新手,并且在使用方法时遇到了问题。 I'm trying to get the method determineAndDisplay to show but what I've tried isn't working.
我正在尝试让方法确定AndDisplay 显示,但我尝试过的方法不起作用。 I get an error when I put things in the parentheses for determineAndDisplay() in main that they cannot be found.
当我将内容放在 main 中的确定AndDisplay() 括号中时,出现错误,无法找到它们。 I'm not certain what I'm getting wrong and any guidance will be of a major help, thanks!
我不确定我做错了什么,任何指导都会有很大帮助,谢谢!
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
You need to enclose both determineAndDisplay() and main() inside a public class like this as almost everything in Java happens within classes.您需要像这样在公共 class 中包含 determineAndDisplay() 和 main() ,因为 Java 中的几乎所有内容都发生在类中。
public class MainClass{
public static void determineAndDisplay(Scanner userInput, int pNum){
// your code
}
public static void main(String[] args){
// your code
}
}
Also, make sure your main method is always inside a public class that has the same name as the file of your code.此外,请确保您的主要方法始终位于与您的代码文件同名的公共 class 中。 Also, make sure that you've imported Scanner.
另外,请确保您已导入扫描仪。 Pass an int and a Scanner object to your determineAndDisplay() method call.
将 int 和 Scanner object 传递给您的确定AndDisplay() 方法调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.