Question

It needs to be done in JAVA

Please don't use the flag=true method. I need it done in another way.

The user should input the number of numbers they are going to enter. Your job is to find the maximum and the minimum of these

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code:

import java.util.Scanner;
public class Main{
   public static void main(String[] args){
   Scanner input=new Scanner(System.in);
   //asking for number of elements
   System.out.print("Enter the number of elements: ");
   //accepting number of elements
   int n=input.nextInt();
   //asking for elements
   System.out.print("Enter the elements: ");
   //initializing min,max and number
   int min,max,num;
   //accepting first number
   num=input.nextInt();
   //setting it as min
   min=num;
   //setting it as max
   max=num;
   //loop to accept all numbers
   for(int i=1;i<n;i++){
   //checking if the number is less than 0
   if(num<0){
   //printing Program aborted
   System.out.println("Program aborted due to the entrance of a negative number");
   //exiting Program
   return;
   }
   //checking if the number is minimum
   if(num<min)
   //setting number as minimum
   min=num;
   //checking if the number is maximum
   if(num>max)
   //setting number as maximum
   max=num;
   //accepting next number
   num=input.nextInt();
   }
   //checking if the number is less than 0
   if(num<0){
   //printing Program aborted
   System.out.println("Program aborted due to the entrance of a negative number");
   //exiting Program
   return;
   }
   //checking if the number is minimum
   if(num<min)
   //setting number as minimum
   min=num;
   //checking if the number is maximum
   if(num>max)
   //setting number as maximum
   max=num;
   //printing minimum, maximum and difference
   System.out.println("min= "+min+" max= "+max+" and the diff is "+(max-min)+".");
   }
}


Screenshot:

1 import java.util.Scanner; 2. public class Main{ 3 public static void main(String[] args) { Scanner input=new Scanner(System

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 } num=input.nextInt(); } //checking if the number is less than
Input and Output:

Enter the number of elements: 3 Enter the elements: 1 2 4 min= 1 max= 4 and the diff is 3. =

Enter the number of elements: 3 Enter the elements: 4 -5 Program aborted due to the entrance of a negative number

Add a comment
Know the answer?
Add Answer to:
It needs to be done in JAVA Please don't use the flag=true method. I need it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

  • Create two java programs. The first will ask the user to enter three integers. Once the...

    Create two java programs. The first will ask the user to enter three integers. Once the three integers are entered, ask the user to enter one of three functions. The “average” if they want the average of the three numbers entered, enter “min” if they want the minimum and “max” if they want the maximum. Display the answer. Ask the user if they want to calculate another function for the three numbers. If so, loop and ask what function they...

  • I need this done in java plz, add comments for every single method. also, make as simple as po...

    i need this done in java plz, add comments for every single method. also, make as simple as possible. 3. Write a GUI for computing the statistics of numbers input from the user. The GUI should have a text field and a button for adding floating point numbers. Each time a number is entered have a label show the current max, min and average. Have your GUI accept numbers whether the button is pressed or the enter key is pressed...

  • We use JAVA. Thanks.    Create an application whose main method asks the user to enter...

    We use JAVA. Thanks.    Create an application whose main method asks the user to enter an n by m integer matrix that contains nm integer numbers. n and m should be between 1 and 10. If the user enters a             number less than 1 or greater than 10, the program will continue to ask the user to enter an integer number between 1 and 10. The program should print the sum of the boundary elements of the matrix....

  • hi, please use spyder formatting or python 3 accessible Apps M Gmail Maps YouTube Python 3...

    hi, please use spyder formatting or python 3 accessible Apps M Gmail Maps YouTube Python 3 1h 51m left 1. Multiplying with a Sentinel and Branching Negative ALL Write a program that will calculate the product of user-specified negative numbers, in succession, until the user enters done. And then display the final product. Zero is considered positive and the initial product should set to 1. Here is an example run of the program: Enter a number or done to quit:...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • I need help with this problem please, This code needs to be in Ruby, Java, or...

    I need help with this problem please, This code needs to be in Ruby, Java, or Python. See problem below. -5 - Functional Programming (due Mon] Assignment Content Your software company was invited to provide a proposal for a company in Australia. You currently have the cost in US dollars and need to convert the prices to the Australian dollar. Write a 2-part program using Ruby, Java, or Python. Part 1: Write a function to gather the following costs from...

  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • Hi, I need help with a fixed-point method program. It has to be done in C. The inputs are: Initia...

    Hi, I need help with a fixed-point method program. It has to be done in C. The inputs are: Initial approximation ρo, tolerance ε, and the maximum number of iterations MAXIT. The outputs are: Approximate solution ρ or "solution not found". STEP 1 Do i=1 STEP 2 While i≤ MAXIT, execute steps 3-6 STEP 3 Do ρ=ϕ(p) (calculate pi) STEP 4 If |ρ-ρo|<ε then output(ρ) (operation sucessful) END STEP 5 Do i=i+1 STEP 6 Do po=p (update po) STEP 7...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT