6.16 LAB: Find largest number (EO) Write a method, findMax(), that repeatedly reads in integers until a negative integer is read and keeps track of the largest integer that has been read. findMax() then returns the largest number entered. Ex: If the input is: 2 77 17 4 -1 the findMax() returns and the program output is: 77 Assume a user will enter at least one non-zero integer. Note: Your program must define the method: public int findMax()

Respuesta :

Answer:

Explanation:

The following code is written in Java, it creates and tests the findMax() method with the test inputs provided in the question. The method loops through asking the user for inputs, if a positive number is passed it checks if it is larger than the previous numbers and if so, saves it in a variable called max. Otherwise it breaks the loop and returns the variable max. The test output can be seen in the attached picture below.

import java.util.Scanner;

class Brainly {

   public static void main(String[] args) {

       System.out.println("Max number is: " + findMax());

   }

   public static int findMax() {

       int max = 0;

       Scanner in = new Scanner(System.in);

       while (true) {

           System.out.println("Enter a number: ");

           int num = in.nextInt();

           if (num >= 0) {

               if (num > max) {

                   max = num;

               }

           } else {

               break;

           }

       }

       return max;

   }

}

Ver imagen sandlee09

Answer:

import java.util.Scanner;

public class LabProgram {

  public static void main(String[] args) {

      System.out.println(findMax());

  }

  public static int findMax() {

      int max = 0;

      Scanner in = new Scanner(System.in);

      while (true) {

          int num = in.nextInt();

          if (num >= 0) {

              if (num > max) {

                  max = num;

              }

          } else {

              break;

          }

      }

      return max;

  }

}

Explanation: this is the zybooks answer