Finding Max value in for Loop

utnicho :

I'm a beginner at java and was just wondering how to find the max and minimum value from integers declared in a for loop (using a scanner to obtain user input) this program creates an object from a class Car and obtains information on the name, registration, colour and number of trips.

The number of trips prompts the for loop to print out odometer readings from 0 (odometer's initial reading) to whatever the variable carSample.numberOfTrips specifies.

I've tried declaring a new variable; int maximum = carSample.odometerReading.MAX_VALUE; (Then printing it) aswell with minimum, however to no success; I receive the following error:

TestCar.java:25: error: int cannot be dereferenced int maximum = carSample.odometerReading.MAX_VALUE;

public static void main(String[] args){
Scanner input = new Scanner(System.in);
    car carSample = new car(); // Creates object of class Car

carSample.name = input.nextLine();
carSample.registration = input.nextLine();
carSample.colour = input.nextLine();
carSample.numberOfTrips = input.nextInt();

for (int i = 0; i < carSample.numberOfTrips; i++) {
    System.out.print("Odometer reading " + (i) + ": ");
    int odometerReading = input.nextInt();
}

Any help or insight into how this can be peformed is really appreciated, thank you for your time!

Fatih Develi :
int maximum = Integer.MIN_VALUE;
for (int i = 0; i < carSample.numberOfTrips; i++) {
    System.out.print("Odometer reading " + (i) + ": ");
    int odometerReading = input.nextInt();
    if (odometerReading > maximum) {
        maximum = odometerReading;
    }
}

System.out.println(maximum); // Maximum value

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=477720&siteId=1