标签:print imp for循环 style ati lse ++ rgs string
package test2; import java.util.Scanner; /* * 寻找水王 */ public class test2 { public static void main(String[] args) { int[] a = null;//存储帖子id Scanner sc = new Scanner(System.in); System.out.println("请输入帖子总个数:"); int num = sc.nextInt(); a = new int[num]; int d=0;; for(int i=0;i<num;i++){ d=i+1; System.out.print("请输入第"+d+"个帖子数:"); a[i]=sc.nextInt(); } int id = 0; int c=0; for(int i=0;i<a.length;i++) { if(c==0) { id=a[i]; c=1; }else if(c>0){ if(a[i]==id) { c=c+1; }else if(a[i]!=id){ c=c-1; } } } System.out.println("水王的ID为 : " + id); } }
这次是只遍历一次,通过一次for循环,来找到水王
标签:print imp for循环 style ati lse ++ rgs string
原文地址:https://www.cnblogs.com/zlj843767688/p/10962734.html