Dan array. Determine the number of maximum elements in the array

The first line sets the number of elements in the array (no more than 100)

The second line introduces the elements of the array.


I know how to find the maximum value in an array with random numbers, but how to push elements from the keyboard into the array (as in a task) if they are written in a space separated string And then how to work with them?

  • Tasks better to lead the text. - Qwertiy ♦
  • And what does "the number of maximum elements" mean? - LEQADA
  • @LEQADA how many times the maximum element occurs in the array - Nikita
  • Read about Scanner in Java. With it, you will enter the elements from the keyboard. - LEQADA pm
  • @LEQADA how to enter elements is understandable, but how to push them into an array? - Nikita

3 answers 3

I will answer the question "How to fill an array of elements entered from the keyboard." For this we need a Scanner .

 public static void main(String[] args) { Scanner input = new Scanner(System.in); // ОбъявляСм Scanner System.out.println("Enter array length: "); int size = input.nextInt(); // Π§ΠΈΡ‚Π°Π΅ΠΌ с ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρ‹ Ρ€Π°Π·ΠΌΠ΅Ρ€ массива ΠΈ записываСм Π² size int array[] = new int[size]; // Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ массив int Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ Π² size System.out.println("Insert array elements:"); /*ΠŸΡ€ΠΎΠΉΠ΄Ρ‘ΠΌΡΡ ΠΏΠΎ всСму массиву, заполняя Π΅Π³ΠΎ*/ for (int i = 0; i < size; i++) { array[i] = input.nextInt(); // ЗаполняСм массив элСмСнтами, Π²Π²Π΅Π΄Ρ‘Π½Π½Ρ‹ΠΌΠΈ с ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρ‹ } System.out.print ("Inserted array elements:"); for (int i = 0; i < size; i++) { System.out.print (" " + array[i]); // Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π½Π° экран, ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹ΠΉ массив } System.out.println(); } 

And then work with the array in the same way as you used to work with an array filled with random numbers.

    So, it seems prettier!?

     public static void main(String[] agrs) { int[] a = new int[7]; int i = 0; Scanner in = new Scanner(System.in); for (int element : a) { System.out.print("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ " + (i + 1) + "-ΠΉ элСмСнт массива: "); a[i] = in.nextInt(); System.out.println(a[i] + " - "); i++; } for (int element : a) System.out.print(element + " "); } 

    Or so:

     public static void main(String[] agrs) { int[] a = new int[7]; int i = 0; Scanner in = new Scanner(System.in); for (int element : a) { System.out.print("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ " + (i + 1) + "-ΠΉ элСмСнт массива: "); a[i] = in.nextInt(); System.out.println(a[i] + " - "); i++; } System.out.println(Arrays.toString(a)); } 
    • And how is your code related to the question? You simply set the elements in the array from the keyboard without further action. - Denis
    • @Denis and what are the next steps? - Nick Volynkin ♦
    • @ Igrr you declare an array [7] , and you need an array of length specified by the number from the first line. - Nick Volynkin ♦
    • @NickVolynkin find the number of maximum elements the same. In general, yes, I didn’t read the question very well, the difficulty was to enter the elements of the array from the keyboard) - Denis
     import java.util.Scanner; public class PerBor_Mas { public static void main(String[] args) throws InterruptedException { int n = vvod.nextInt(); int[] A = new int[n]; int k = vvod.nextInt(); Scanner vvod = new Scanner(System.in); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π΄Π»ΠΈΠ½Ρƒ ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива"); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ элСмСнт массива"); System.out.println("Водится массив"); for (int i = 0; i < n; i++) { System.out.print(A[i] + k + "\t"); k++; } System.out.println(" "); } } 
    • Entering a one-dimensional array from the keyboard. - TaraBar Barb
    • Use the edit button to edit the answer. - 0xdb