# How to write a program that finds the number 4 from the keyboard and displays the largest of them?

Actually a subject.
You need to write a program that finds the number 4 from the keyboard and displays most of them. I don't quite understand how to do it.

I have the code

``````package code;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int a;
int b;
int c;
int max;
System.out.println("Iput A");
a = input.nextInt();
System.out.println("Iput B");
b = input.nextInt();
System.out.println("Iput C");
c = input.nextInt();

max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}

System.out.println("Max =" + max);
}
}
}``````

There actually all very clear, but there is a job in which to do SUBJECT. I understand you want to restrict the input numbers 4 numbers if the numbers will be less than 4 or more than 4 digits will be displayed warning the user that need to enter 4 numbers. Thank you all for the help.
March 23rd 20 at 19:30
March 23rd 20 at 19:32
Here is my version in one expression
``````IntStream.range(0, 4)
.mapToObj(i -> System.in)
.map(Scanner::new)
.map(Scanner::nextInt)
.reduce(Integer::max)
.ifPresent(System.out::println);``````
Thank you very much for your help and for your attention! - Alexandra_Farrell commented on March 23rd 20 at 19:35
March 23rd 20 at 19:34
Make a cycle from 0 to 3 and give him reading into an array and sort in descending order. The first number is the maximum.
Please if you wouldn't mind, could you write an example? - Alexandra_Farrell commented on March 23rd 20 at 19:37
@Alexandra_Farrell, hardly, I don't Gabest. - dashawn.Pagac commented on March 23rd 20 at 19:40
@dashawn.Pagac, Ahaha well, let's see what happens :D - Alexandra_Farrell commented on March 23rd 20 at 19:43
@Alexandra_Farrell,
``````public static void bubbleSort(int[] arr){
for(int i = arr.length-1 ; i > 0 ; i--){
for(int j = 0 ; j < i ; j++){
/*Compare the elements in pairs,
if they have the incorrect order
then swap*/
if( arr[j] > arr[j+1] ){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}

int[3] num;
for (int i = 0; i < 3; i++){
System.out.println("Iput" + num[i] + " number:");
num[i] = input.nextInt();
}
bubbleSort(num);
System.out.println("Max is" + num[0]);``````

Are not responsible for the accent, the code will not work if it simply skopipastit. - dashawn.Pagac commented on March 23rd 20 at 19:46
Thank you very much for your help and for your attention! - Alexandra_Farrell commented on March 23rd 20 at 19:49
4 times counting the number at each iteration to remember the number if the new value is greater than the previous. At the end of his display - Dale75 commented on March 23rd 20 at 19:52
March 23rd 20 at 19:36
``````public static void main(String[] args)
{
Scanner io = new Scanner(System.in);
int max = 0;
int cur = 0;
int step = 0;
while(step++ < 4 && io.hasNextInt())
{
cur = io.nextInt();
if(cur > max) max = cur;
}
System.out.printf("Max: %d", max);
}``````
Thank you very much for your help and for your attention! - Alexandra_Farrell commented on March 23rd 20 at 19:39

Find more questions by tags Java