Hi guys, im learning java and i have an assigment to to input 100 number and then show the highest number and when it was inputed (exe, 1,5,2 shows the number 5 and 2 (2 means second input) now ive writting the code and the result is
This is ok,but when i try to put the highest number last it shows as follows:Input a number
15
1
1
1
1
15 Was inputed 1
I understand that the problem is that when the last number is inputed it dosent enter the loop so value dosent get its new "value",but i dont know how to work around it,something is wrong with while part i gusses but i cant figure it outInput a number
1
2
3
4
5
4 Was inputed 4
The code:
package page25; import java.util.Scanner; public class Drill9 { public static void main(String[] args) { System.out.println("Input a number"); Scanner scan = new Scanner(System.in); int num = scan.nextInt(); int counter = 1, value = 1, max = 0; while (counter < 5) { if (num >= max) { max = num; value = counter; counter++; } num = scan.nextInt(); } System.out.println(max + " Was inputed " + value); } }
Edited*
edited**