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,159
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,555
Joined: Jul 23 2006
Gold: 1,150.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
the_rest
#3 Jan 8 2019 12:05am
Group: Member
Posts: 35,078
Joined: Nov 29 2005
Gold: 616.00
just because 'first' is a boolean, doesn't mean that the code will break and finish the method. The method will go on even if 'first' is set to false
Go Back To Java, Visual Basic, VBScript Topic List
Add Reply New Topic New Poll