I've spent a while trying to figure this out. It doesn't work if all the numbers entered are negative. The reason is because b is initialized to zero, so if all the numbers are negative, b is never less than a. I've tried different things but none of them have worked. I tried setting b equal to null, but it turns out that this can't be done.
package com.codegym.task.task05.task0532;
import java.io.*;
/*
Task about algorithms
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
int b = 0;
int x;
for(x = 0; x < N; x++){
if(N <= 0) {
break;
}
else{
int a = Integer.parseInt(reader.readLine());
if(b < a){
b = a;
}
}// else
}// for loop
if(N > 0){
int maximum = b;
System.out.println(maximum);
}
}
}
/*
*********************************************************************************************
* The maximum initialization had to be moved from under the b initialization to right before
* the print statement, otherwise maximum would always be 0.
*********************************************************************************************
*/