Packagehomework in class;Importjava.util.InputMismatchException;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) { for(intM=0;;) { intJudge=0; Scanner in=NewScanner (system.in); intN; System.out.println ("Please enter the number of posts:"); N=In.nextint (); System.out.println ("Please enter an ID (ordinal number is an integer):"); intid[]=New int[n]; Try { for(inti=0;i<n;i++) {Id[i]=In.nextint (); } } Catch(inputmismatchexception e) {System.out.println ("The input is illegal!" "); Judge=1; } intSHUIWANG=ID[0];intTemp=0; for(inti=0;i<n-1;i++) { if(shuiwang==id[i+1]) {temp++; } Else{Temp--; } if(temp<0) {Shuiwang=id[i+1]; } } if(judge!=1) System.out.println ("Water King is:" +Shuiwang); System.out.println ("Keep looking for water king Press any key, exit please press S:"); String s=In.next (); if(S.equals ("s")) System.exit (0); Else Continue; In.close (); } }}
Results:
Practice in class