Reputation: 77
I'm new to Java and having trouble with methods. I'm trying to get the method determineAndDisplay to show but what I've tried isn't working. I get an error when I put things in the parentheses for determineAndDisplay() in main that they cannot be found. 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
Upvotes: 1
Views: 33
Reputation: 167
You need to enclose both determineAndDisplay() and main() inside a public class like this as almost everything in Java happens within classes.
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. Also, make sure that you've imported Scanner. Pass an int and a Scanner object to your determineAndDisplay() method call.
Upvotes: 1