Type to search

A Java Maximum And Minimum Program

Java Tutorial

A Java Maximum And Minimum Program

Share

This is A Java Program That Picks The Maximum And Minimum From A Set Of Numbers

While this is a part of the tricks to learn Java programming quickly, I believe that the idea can be used in other programming languages as well.

See and read the code below before we get to the explanation.

READ AlSO What Are Some Tricks To Learn Java Quickly?

 

import java.util.Random;

import java.util.Scanner;

 

public static void main(String[] args) {

Random generator = new Random();

Scanner sc = new Scanner(System.in);

int num = generator.nextInt(100) + 1;

int max = num;

int maxposition = 0;

System.out.println(num);

 

for   ( int i = 2; i < 11; i ++) {

num = generator.nextInt(100) + 1;

if (num > max) {

max = num;

maxposition = i;

}

}

System.out.println(“The Maximum Number Is: ” + max + ” and Its Position Is: ” + maxposition);

 

}

Hmm, that is a bit longer than my previous tutorials nevertheless, I still have to explain each line of code. So come along with me.

The first and second line are utilities in Java language. We import them whenever there is need to add any of them to our code.

import java.util.Random;

This is the Random utility; it is used when we want to generate random values. More explanation in a sec.

import java.util.Scanner;

Similar to Random in the sense that we import it also but its own usage is different. We call the Scanner utility when we want to scan to the screen that is when we want to display something on the screen.

The imported utilities are placed in the Java package section of the code.

Random generator = new Random();

READ ALSO ABOUT  Operators In Java (ALL) In A Simple Guide

After importation, the next thing is the usage. Just like when you buy and import an item from overseas. The next thing is to use it, or you can sell it to someone that will use. J

This line of code is used to bring the Random utility to a specific part of our code where it will be USEFUL.

The generator in that statement is the name given to the Random utility for that part of the program.

You will use a different name if there is need to use the utility in another part of the program.

Scanner sc = new Scanner(System.in);

To Be Continued…

A Java Maximum And Minimum Program

Leave a Comment

Your email address will not be published. Required fields are marked *

error: