d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Please Explain This Code In Depth
Add Reply New Topic New Poll
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Nov 8 2018 07:36pm
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
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 8 2018 07:50pm
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
Member
Posts: 36,244
Joined: Nov 29 2005
Gold: Locked
Trader: Scammer
Jan 8 2019 12:05am
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 Programming & Development Topic List
Add Reply New Topic New Poll