[英]Can't get output for method
我是 Java 的新手,並且在使用方法時遇到了問題。 我正在嘗試讓方法確定AndDisplay 顯示,但我嘗試過的方法不起作用。 當我將內容放在 main 中的確定AndDisplay() 括號中時,出現錯誤,無法找到它們。 我不確定我做錯了什么,任何指導都會有很大幫助,謝謝!
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
您需要像這樣在公共 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
}
}
此外,請確保您的主要方法始終位於與您的代碼文件同名的公共 class 中。 另外,請確保您已導入掃描儀。 將 int 和 Scanner object 傳遞給您的確定AndDisplay() 方法調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.