1. It is necessary to compare the numbers in the array and display the largest one. The code does it.
  2. Problem: negative numbers are incorrectly compared. I do not understand why.
  3. If there are several maximum numbers, you need to display it.

Help to understand, please.

public static void main(String[] args) throws Exception { InputStream inputStream = System.in; Reader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); int [] array = new int[4]; int max = 0; int eq = 0; for (int i = 0; i < 4; i++) { array[i] = Integer.parseInt(bufferedReader.readLine()); } for(int i = 0; i < 4; i++) { if (Math.abs(array[i]) > max){ max = array[i]; } } System.out.println(max); } 
  • four
    Ask what the Math.abs function Math.abs ... - Akina
  • What does it mean "negative numbers are incorrectly compared"? Give an example of incorrectness - Alexander Chernin

2 answers 2

The problem is in this block of code.

  if (Math.abs(array[i]) > max){ max = array[i]; } 

You compare the absolute value of the array[i] element with the non-absolute value of the variable max . I assume that you are looking for an element in the array for the maximum absolute value. Otherwise, in general, it is not clear why you use the abs method.

At least you should write

  if (Math.abs(array[i]) > Math.abs( max ) ){ max = array[i]; } 

In addition, this algorithm is usually performed as follows. First, max is set equal to the value of array[0] . Then use a loop, starting with a loop count of 1.

Here is a minimal demo program that shows how to find the maximum in absolute value and just the maximum.

 import java.util.*; import java.lang.*; import java.io.*; class Ideone { public static void main (String[] args) throws java.lang.Exception { int[] array = { -100 , -5, 80, 999, 666, -3333 }; int max_abs = array[0]; int max = array[0]; for ( int i = 1; i < array.length; i++ ) { if ( Math.abs( max_abs ) < Math.abs( array[i] ) ) { max_abs = array[i]; } if ( max < array[i] ) { max = array[i]; } } System.out.println( "Absolute maximum value is " + Math.abs( max_abs ) ); System.out.println( "Maximum value is " + max ); } } 

The output of the program to the console:

 Absolute maximum value is 3333 Maximum value is 999 

Compare the results.

As for this item

3.If there are several maximum numbers, you need to display it.

then you should put it in a separate question. That is, in one question, deal with negative numbers, and in another question, solve a completely different problem by providing the appropriate code, and indicating that you do not succeed. Most likely you will need to use an ArrayList for this purpose.

  • and among the array -100, -5, 80.99999,666, -3333 the largest will be -3333 .... - Max Krugovykh
  • @MaxKrugovykh That's right, since the absolute maximum value is searched for, as follows from the author's code. - Vlad from Moscow
  • T / C 1. It is necessary to compare the numbers in the array and display the largest one. - Max Krugovykh
  • as it follows from the author’s code. But this does NOT follow from the text of the assignment. The author probably just copied someone else's solution to another problem. And now he is trying to adjust it for himself. - Akina
  • @Akina It's hard to figure out what the task actually was. I look at the code, and not on the fact that, at times, novice programmers can not properly duplicate the task. - Vlad from Moscow

According to your logic, you compare the modulus of a number with a number, which is not good. set the max variable to array [0]; and in comparison starting from i = 1 , compare the elements

Ps. By assigning 0 to the element of the value of the first element of the array, you solve the problem with negative numbers

  • In this code - will not solve. - Enikeyschik
  • I'm not talking about this code, but about what I wrote. For it, to avoid negative for some reason, use abs - Max Krugovykh
  • So how do you do it? - k-paxian
  • @ k-paxian change the code: int max = array [0]; and for (int i = 1; i <4; i ++) {if (array [i]> max) {max = array [i]; }} - Max Krugovykh
  • @ k-paxian The rest is correct more or less - Max Krugovykh