Hi!
Can somebody give me a hint about what I'm doing wrong?
package pl.codegym.task.task05.task0532;
import java.io.*;
/*
Zadanie z algorytmami
*/
public class Solution {
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
Reader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int n = Integer.parseInt(bufferedReader.readLine());
int maksimum = 1;
if(n > 0)
{for(int i = 0; i <= n; i++)
{
int a = Integer.parseInt(bufferedReader.readLine());
if(a != 0)
{
if(a > maksimum)
{
maksimum = a;
}
}
}
//tutaj wpisz swój kod
System.out.println(maksimum);
}
}
}
for
loop reads one number more than it should read. Loop condition should be:i < n
(not:i <= n
). When thefor
loop runs for the first time you should set variablemaksimum
to the value of the first number entered from the keyboard. If the numbers entered within the for loop are all below zero yourmaksimum
(initialized with value 1) will always be bigger and will be displayed as a result (in line 33). And theif
condition in line 20 of your code should be removed. Why do you check if the number entered is different than 0 (if (a != 0)
)?