package mypackage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.io.StreamTokenizer; public class main { static String id[]=new String[1000]; static int num=0; public static void setwater(String id[],int a) { int j=0; for(int i=0;i<=a;i=i+j) { if(num>0) { if(id[0]==id[1]) {id[1]=id[i+2];num++;j=1;} if(id[0]!=id[1]) {id[1]=id[i+2];num--;j=1;} } else { if(id[0]==id[1]) {id[0]=id[i];id[1]=id[i+2];num++;j=1;} if(id[0]!=id[1]) {id[0]=id[i+2];id[1]=id[i+3];j=2;} } } System.out.println("水王是:"+id[0]); } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Reader r = new BufferedReader(new FileReader("C:\\Users\\xxoo\\Desktop\\新建文本文档.txt")); StreamTokenizer stok = new StreamTokenizer(r); stok.parseNumbers(); // int[] sum=new int [1000]; int i=-1; stok.nextToken(); while (stok.ttype != StreamTokenizer.TT_EOF) { if (stok.ttype == StreamTokenizer.TT_NUMBER) { i++; // sum[i] = (int) stok.nval; } else { i++; id[i]=stok.sval; // System.out.println("Nonnumber: " + stok.sval); stok.nextToken(); } }//读取 setwater(id,i); } }