d2jsp
d2jsp Forums > Programmer's Haven > Java, Visual Basic, VBScript > Please Explain This Code In Depth
Add Reply New Topic New Poll
ferf
#1 Nov 8 2018 07:36pm
Group: Member
Posts: 30,077
Joined: Mar 25 2009
Gold: 0.00
Code
package com.company;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int min = 0;
int max = 0;

boolean first = true;


while(true) {
System.out.println("Enter Number:");
boolean isAnInt = scanner.hasNextInt();
if(isAnInt) {
int number = scanner.nextInt();
if(first) {
first = false;
min = number;
max = number;
}
if(number > max) {
max = number;
}
if(number < min) {
min = number;
}
} else {
break;
}

scanner.nextLine(); //handle input
}

System.out.println("Min = " + min + ", max = " + max);

scanner.close();

}
}




I'm not understanding this at all.... how it works... if someone wouldn't mind helping me out, i'd appreciate it, ty guys!
Mostly i don't get how it's figuring out which number is max and which is minimum value... i don't get how it's reasoning that

This post was edited by ferf on Nov 8 2018 07:37pm
carteblanche
#2 Nov 8 2018 07:50pm
Group: Member
Posts: 32,212
Joined: Jul 23 2006
Gold: 269.00
Quote
Mostly i don't get how it's figuring out which number is max


if(number > max) {
max = number;
}



This post was edited by carteblanche on Nov 8 2018 07:56pm
Go Back To Java, Visual Basic, VBScript Topic List
Add Reply New Topic New Poll