简体   繁体   中英

Can't get output for method

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM