243,873 views
14 votes
14 votes
Java application that inputs a series of 10 integers and determines and prints the largest integer.

User Shahab
by
2.8k points

1 Answer

9 votes
9 votes

Answer:

import java.util.Scanner;

class Main {

static final int TOTAL = 10;

public static void main (String args[])

{

Scanner input = new Scanner(System.in);

int largest = 0;

for(int count=1; count<=TOTAL; count++) {

System.out.printf("Enter number %d of %d : ", count, TOTAL);

int number = input.nextInt();

if (count == 1) {

largest = number;

} else {

largest = Math.max(largest, number);

}

}

System.out.printf("The largest number is %d\\", largest);

input.close();

}

}

Step-by-step explanation:

You have to pay special attention to the initialization of largest, because the initialized value should not be part of the competition of which number is the largest. That's why if (count==1) is a special case.

User Jay Kim
by
2.8k points