问题:
boyException in thread "main" java.util.IllegalFormatConversionException: d != Linkedlist.Boy
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2938)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2892)
at java.base/java.util.Formatter.format(Formatter.java:2673)
at java.base/java.io.PrintStream.format(PrintStream.java:1053)
at java.base/java.io.PrintStream.printf(PrintStream.java:949)
at Linkedlist.CircleSingleLinkedList.countBoy(Joseph.java:86)
at Linkedlist.Joseph.main(Joseph.java:10)
为什么老是跑出异常?TT
```java
public void showBoy() {
if (first == null) {
System.out.println("there is no boy!");
return;
}
Boy curBoy = first;
while (true) {
System.out.printf("the boy's No is %d \n", curBoy.getNo());
if (curBoy.getNext() == first) { //get the last No ,the traverse is finished
break;
}
curBoy = curBoy.getNext();//move it
}
}
public void countBoy(int startNo, int count, int num) {
if (first == null || startNo < 1 || startNo > num) {
System.out.println("the parameters is wrong,please enter it again");
return;
}
Boy helper = first;//创建一个辅助指针,帮小孩出圈
while (true) {
if (helper.getNext() == first) {//事先让helper指向最后一个节点
break;
}
helper = helper.getNext();
}
//小孩报数前,先让first和helper移动 k-1 次
for (int j = 0; j < startNo - 1; j++) {
first = first.getNext();
helper = helper.getNext();
}
//当小孩报数时,first和helper同时移动 m-1 次,然后出圈
//因为这里是一个循环操作,直到圈中只有一个节点
while (true) {
if (helper == first) {//only one node
break;
}
for (int j = 0; j < count - 1; j++) {//move (k - 1) node
first = first.getNext();
helper = helper.getNext();
}
System.out.printf("boy %d should go out \n", first.getNext());
first = first.getNext();
helper.setNext(first);//helper的指针域指向first 形成一个新环
}
System.out.printf("the last boy left in the CLL is %d \n", helper.getNext());
}
```