About 6,250,000 results
Open links in new tab
  1. java - Finding maximum number from a loop - Stack Overflow

    Feb 6, 2012 · If the number has the same value as (==) max, then you need to add 1 to count_max. The last If block needs a slight tweak too. If you find a new highest value (ie, number > max, not the other way around), then you need to reset your count_max to 1, not add 1 to it.

  2. java - Finding the maximum value from a for loop - Stack Overflow

    May 2, 2017 · Find the Max/Min of values within a for loop. Save your product for a later comparison. If you found a palindrom compare it to the last highest palindrom. If it is higher than the saved value store the new value as last highest palindrom. Store the according i and j …

  3. for loop - Java highest score - Stack Overflow

    Sep 27, 2015 · int highest = 0; num = x.nextLine(); largest = num; for (int i = 0; i <= 5; i++) { System.out.print("Enter name: "); k = x.nextInt(); System.out.print("Enter score: "); num = x.nextInt(); if (num > highest) { highest = num; System.out.println(largest); } // how do i display the name of the highest score and the second placer?

  4. Java Math - W3Schools

    The Java Math class has many methods that allows you to perform mathematical tasks on numbers. The Math.max(x, y) method can be used to find the highest value of x and y: The Math.min(x, y) method can be used to find the lowest value of x and y: The Math.sqrt(x) method returns the square root of x:

  5. For-Each Loop in Java - GeeksforGeeks

    Apr 14, 2025 · Finding Maximum in an Array using for-each Loop. Example: This example demonstrates how to find the maximum value in an integer array using a for-each loop. Explanation: In the above example, we create a method findMax ().

  6. Java Program to Return the Largest Element in a List

    Jan 18, 2023 · Given a List, find the largest element in it. There are multiple approaches to tackle this problem, such as iterating through the List or using various inbuilt functions. Output : 234. Input : List = {10, 20, 4} Output : 20. Approach 1: Using a ForEach Loop. Create List object and store multiple elements in it.

  7. find highest and lowest of five integers using java loops

    Nov 25, 2020 · //Declare max as Minimum and min as Maximum int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; //Declare Scanner to take input from user Scanner sc = new Scanner (System.in); System.out.print ("Enter 5 integers: "); //for loop that runs 5 times to take input from user for (int i = 0; i < 5; ++i) { int num = sc.nextInt (); //Check if t...

  8. Find highest number using if statement in a for loop java

    Nov 8, 2014 · You should declare a max variable and initialize it with some very low value. For example Double.MIN_VALUE. Then in every iteration of the loop you could update your maximum like this: max = Math.max(max, valueYouWantToCompare); Finding the correct Java syntax is your task now :-) Good luck!

  9. Java Program to Find Largest Element in an Array

    Apr 9, 2025 · In this article, we will explore four practical approaches one by one to solve this in Java. Example Input/Output: Below are the 4 main approaches: 1. Iterative Approach. The most common method to find and print the largest element of a Java array is to iterate over each element of the array and compare each element with the largest value.

  10. Calculating min and max with if-statements within a for-loop

    Nov 19, 2013 · Here, I am calculating the max and min for distance (which is miles traveled), MPG, and price per gallon. I'd like a general review. new AnnualFuelUse (2, 10, 6800, 7052, 8.10, 3.08), new AnnualFuelUse (3, 20, 7052, 7349, 9.20, 3.15)}; if (fillUps[i].getDist() < dMin){ dMin = fillUps[i].getDist(); minDist = dMin; if (fillUps[i].getDist() > dMax) {

Refresh