递归函数输出错误

时间:2017-05-19 00:00:55

标签: java recursion error-handling

我正在编写一个递归城堡的脚本(可扩展),并且遇到以下错误消息:

at sun.nio.cs.SingleByte.withResult(Unknown Source)
at sun.nio.cs.SingleByte.access$000(Unknown Source)
at sun.nio.cs.SingleByte$Encoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.SingleByte$Encoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at Castle.drawBody(Castle.java:62)
at Castle.drawBody(Castle.java:65)

该方法运行良好,直到我改变了似乎是一种大多不相关的方法。这是我的班级:

import java.util.Scanner;
public class Castle {
    final Scanner numScan = new Scanner(System.in);
    int h = 0;
    int l = 0;
    int term = 0;
    public Castle(){
        startRun();
    }
    public void startRun(){
        System.out.println("Height ratio of Castle:");
        h = numScan.nextInt();
        System.out.println("Length ratio of Castle (odd number):");
        l = numScan.nextInt();
        term = (4*(l/2))+ l + 26;
        if(h%2 == 0){
            drawTowers(h/2, 2, l);
        }else{
            drawTowers(h/2, 3, l);
        }
        drawBody((h*3)/2); 
    }
    public void drawTowers(int height, int mod, int length){
        if(height!=0){
            if(height == h/2){
                System.out.print(".,:'");
                drawBetween(length/2, "'");
                System.out.print("^");
                drawBetween(length/2, "'");
                System.out.print("':,.");
                drawBetween(length+8, " ");
                System.out.print(".,:'");
                drawBetween(length/2, "'");
                System.out.print("^");
                drawBetween(length/2, "'");
                System.out.print("':,.\n[");
                drawBetween((8+((length/2)*2))-1, " ");
                System.out.print("]");
                drawBetween(length+8, " ");
                System.out.print("[");
                drawBetween((8+((length/2)*2))-1, " ");
                System.out.println("]");
            }else if(height%mod == 0){
                System.out.println("|[XX|XX]|  _==_  |[XX|XX]|\n|[XX|XX]|_-'\"\"'-_|[XX|XX]|");
            }else{
                System.out.println("|`_ -   |        |-` _   |\n|   `=  |        |  - ` =|");
            }
            drawTowers(--height, mod, length);
        }
    }
    public void drawBody(int size){
        if(size!=0){
            if(term!=0){
                if(term > 8 && term < 19 && size < h){
                    System.out.print("##");
                }else{
                    System.out.print("[]");
                }
                term-=2;
                drawBody(size);
            }else{
                System.out.println();
                term = (4*(l/2))+ l + 26;
                drawBody(--size);
            }
        }
    }
    public void drawBetween(int t, String item){
        if(t!=0){
            System.out.print(item);
            drawBetween(--t, item);
        }
    }
}

第64行是System.out.print("[]"),我编辑的方法是drawTowers()

导致此问题的原因是什么?如何解决?我不允许使用任何循环

0 个答案:

没有答案