Answer on Question #47450, Engineering, Other
Problem.
Find the biggest number from a sequence of input numbers from keyboard.
Hint: use of Scanner class to accept input and hasNextInt() function
Solution.
Code
import java.util Scanner;
public class Main {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int i = 0; // counter
int number; // new number
int max = 0; // max
while (myScanner.hasNextInt()) {
i++;
number = myScanner.nextInt();
if (i == 1) {
max = number;
} else if (number > max) {
max = number;
}
}
System.out.println("Max: " + Integer.toString(max));
}
}Result
3 - 10 4 1 7 - 3 0 end
Max: 7
https://www.AssignmentExpert.com
Comments