简体   繁体   中英

passing methods working with arrays to main

If I have a method that finds the greatest integer in an array. How do I pass the result back to main?

public static int maxNumber(int[] Array) {
   int maxNumber = Array[0];
   for (int i = 1; i < Array.length; i++) {
      if (List[i] > maxNumber) {
         maxNumber = Array[i]; 
      }
      return maxNumber;
   }
}

Return the result outside the loop, it should be this way

public static int maxNumber(int[] Array)
{
     int maxNumber = Array[0];
     for (int i = 1; i < Array.length; i++)
     {
         if (List[i] > maxNumber)
         {
             maxNumber = Array[i];
         }
     }
     return maxNumber;
}

if you want to get the number as output, call like this

   int max = maxNumber(yourArray);

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