本帖最后由 满兴旺 于 2014-5-4 17:49 编辑
BaoShu.zip
(1014 Bytes, 下载次数: 299)
,共同进步- package cn.itcast.test;
- import java.util.*;
- import java.io.*;
- class Person
- {
- private int id;
- Person(int id)
- {
- this.id=id;
- }
- public String toString()
- {
- return "我是第"+id+"个人";
- }
- }
- public class BaoShu
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
-
- public static void main(String[] args) throws Exception
- {
- BufferedReader bufr=
- new BufferedReader(new InputStreamReader(System.in));
-
- System.out.println("请输入要报数的人数(不要太过分哦):");
-
- String personNumber=bufr.readLine();
- int PersonNumber=Integer.parseInt(personNumber,10);
-
- ArrayList<Person> persons=new ArrayList<Person>();
-
- for(int i=0;i<PersonNumber;i++)
- {
- persons.add(new Person(i+1));
- }
-
- sop("您所输入的人数是:"+persons.size());
-
- int count=1;
- int i=0;
- while(i<persons.size()&&persons.size()>1)
- {
- if(count==0)
- {
- sop(persons.get(i)+"报数:"+count+" i:"+i+" size="+persons.size()+"我走了");
- persons.remove(i);
- if(i==persons.size())//这里是为了防止i大于数组中的最大坐标值
- {
- i=0;
- }
- count=(count+1)%3;
- }else
- {
- sop(persons.get(i)+"报数:"+count+" i:"+i+" size="+persons.size());
- i=(i+1)%persons.size();
- count=(count+1)%3;
- }
- }
- sop("接下来请留下来的人自我介绍一下:"+persons);
- }
- }
复制代码
|