Answer the question
In order to leave comments, you need to log in
How to write a program that counts 4 numbers from the keyboard and displays the largest of them?
Actually a subject.
You need to write a program that counts 4 numbers from the keyboard and displays the largest of them. I don't quite understand how to do it.
I have 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);
}
}
}
Answer the question
In order to leave comments, you need to log in
Here is my version in one statement
IntStream.range(0, 4)
.mapToObj(i -> System.in)
.map(Scanner::new)
.map(Scanner::nextInt)
.reduce(Integer::max)
.ifPresent(System.out::println);
Make a loop from 0 to 3 and stick the reading into an array and sort it in descending order. The first number is the maximum.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question