读书人

编译不过啊解决方案

发布时间: 2012-03-09 16:54:57 作者: rapoo

编译不过啊
package T4;
class People{
public int identifier; //编号
public int new_identifier;
public boolean state; //圈内或圈外
static int i = 0;
public People(){
new_identifier = identifier = i = i+1;
state = true;
}
}
class Test4 {
private People[] people;
public Test4(){
people = new People[100];
}
public void gamerunning(){
int peopleinrool = 100;
while(peopleinrool > 1){//当剩下一人,游戏结束
for(int i = 0; i < 100 ;i++){
int newid = 1;
if(people[i].state){
if(people[i].new_identifier%3 == 0){
people[i].state = false;
peopleinrool--;
}
else
people[i].new_identifier = newid++;
}
}
}
for(int i = 0; i < 100; i++)
if(people[i].state == true){
System.out.print(people[i].identifier);
}
}
public static void main(String[] args){
Test4 test4 = new Test4();
test4.gamerunning();
}
}

[解决办法]
people = new People[100];
只是初始化了数组长度
里面的元素没有初始化

[解决办法]

探讨

people = new People[100];
只是初始化了数组长度
里面的元素没有初始化

[解决办法]
楼主你的数组没有初始化,所以想得到某个数组的state的时候就报错了。
楼主不妨参考下如下代码
Java code
import java.util.Scanner;/** * @ClassName: Test4 * @Description: TODO(这里用一句话描述这个类的作用) * @author A18ccms a18ccms_gmail_com * @date 2012-2-3 下午12:30:38 */class Test4 {    private static class Node {         public int no;// 编号         public Node next;// 下一个节点          public Node(int no) {             this.no = no;         }     }      public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         System.out.print("请输入总人数:");         int totalNum = scanner.nextInt();         System.out.print("请输入报数的大小:");         int cycleNum = scanner.nextInt();         Node header = new Node(1);         Node pointer = header;         for (int i = 2; i <= totalNum; i++) {             //指向下一个节点            pointer.next = new Node(i);             //前移            pointer = pointer.next;         }         //形成循环链表 尾部指向头部        pointer.next = header;         // 初始化环形链表结束         System.out.println("以下是出列的顺序:");         while (pointer != pointer.next) {             for (int i = 1; i < cycleNum; i++) {                 //从头开始                pointer = pointer.next;             }             System.out.println(pointer.next.no);            //前进            pointer.next = pointer.next.next;         }         System.out.println(pointer.next.no);     } } 

读书人网 >J2SE开发

热点推荐