以其他班级可以满足的条件结束程序

时间:2019-06-27 17:43:08

标签: java recursion stack-overflow

所以我的程序是一个随机的地牢生成器,但是我一直遇到堆栈溢出错误,我没有足够的空间分配给我的项目,因此我需要一种在满足一个条件时结束程序的方法< / p>

我已经尝试过在多个类中使用扫描,并且我已经扩展了程序可以用来生成随机的《龙与地下城》地下城的空间,但是我不知道如何根据以下条件终止程序在单独的班级见面。 该条件是程序运行时调用该类的次数

这是该类代码的一部分,我需要将其执行的次数设为条件

import java.util.Random;

public class RandomChamberGenerator {

 public void main() {
  Random rand1 = new Random();
  NormalChamber NCE = new NormalChamber();
  LargeChamber LCE = new LargeChamber();
  if ((rand1.nextInt(20) + 01) <= (01)) {
   System.out.println("square 20x20 ft");
   NCE.main();
  } else {
   if ((rand1.nextInt(20) + 01) <= (2)) {
    System.out.println("square 20x20 ft");
    NCE.main();
   } else {

这是要查找的次数的类的名称 执行RandomChamberGenerator并结束程序

public class EndDungeon {

}

这是NCE.main();

import java.util.Random;
public class NormalChamber {
 public void main() {
  Random Exit = new Random();
  ExitLocation1 EL1 = new ExitLocation1();
  if (Exit.nextInt(20) + 01 <= (01)) {
   System.out.println("0 Exits");
   System.out.println("Things may become rough");
  } else {
   if (Exit.nextInt(20) + 01 <= (2)) {
    System.out.println("0 Exits");
    System.out.println("Things may become rough");
   } else {
    if (Exit.nextInt(20) + 01 <= (3)) {
     System.out.println("0 Exits");
     System.out.println("Things may become rough");
    } else {
     if (Exit.nextInt(20) + 01 <= (4)) {
      System.out.println("0 Exits");
      System.out.println("Things may become rough");
     } else {
      if (Exit.nextInt(20) + 01 <= (5)) {
       System.out.println("0 Exits");
       System.out.println("Things may become rough");
      } else {
       if (Exit.nextInt(20) + 01 <= (6)) {
        System.out.println("1 Exit");
        EL1.main();
       } else {
        if (Exit.nextInt(20) + 01 <= (7)) {
         System.out.println("1 Exit");
         EL1.main();
        } else {
         if (Exit.nextInt(20) + 01 <= (8)) {
          System.out.println("1 Exit");
          EL1.main();
         } else {
          if (Exit.nextInt(20) + 01 <= (9)) {
           System.out.println("1 Exit");
           EL1.main();
          } else {
           if (Exit.nextInt(20) + 01 <= (10)) {
            System.out.println("1 Exit");
            EL1.main();
           } else {
            if (Exit.nextInt(20) + 01 <= (11)) {
             System.out.println("1 Exit");
             EL1.main();
            } else {
             if (Exit.nextInt(20) + 01 <= (12)) {
              System.out.println("2 Exits");
              EL1.main();
              EL1.main();
             } else {
              if (Exit.nextInt(20) + 01 <= (13)) {
               System.out.println("2 Exits");
               EL1.main();
               EL1.main();
              } else {
               if (Exit.nextInt(20) + 01 <= (14)) {
                System.out.println("2 Exits");
                EL1.main();
                EL1.main();
               } else {
                if (Exit.nextInt(20) + 01 <= (15)) {
                 System.out.println("2 Exits");
                 EL1.main();
                 EL1.main();
                } else {
                 if (Exit.nextInt(20) + 01 <= (16)) {
                  System.out.println("3 Exits");
                  EL1.main();
                  EL1.main();
                  EL1.main();
                 } else {
                  if (Exit.nextInt(20) + 01 <= (17)) {
                   System.out.println("3 Exits");
                   EL1.main();
                   EL1.main();
                   EL1.main();
                  } else {
                   if (Exit.nextInt(20) + 01 <= (18)) {
                    System.out.println("3 Exits");
                    EL1.main();
                    EL1.main();
                    EL1.main();
                   } else {
                    if (Exit.nextInt(20) + 01 <= (19)) {
                     System.out.println("4 Exits");
                     EL1.main();
                     EL1.main();
                     EL1.main();
                     EL1.main();
                    } else {
                     if (Exit.nextInt(20) + 01 <= (20)) {
                      System.out.println("4 Exits");
                      EL1.main();
                      EL1.main();
                      EL1.main();
                      EL1.main();
                     }
                    }
                   }
                  }
                 }
                }
               }
              }
             }
            }
           }
          }
         }
        }
       }
      }
     }
    }
   }
  }
 }
}

我希望输出结果是这样的

(Continue Strait 20 ft; passage ends in a door
Down one level to Passage 20 ft long
Continue Strait 20 ft,side passage to the right, then an aditional 10ft ahead
Continue Strait 20 ft,side passage to the left, then an aditional 10ft ahead
Down one level to Passage 20 ft long
Continue Strait 30 ft,no doors or side passages
Continue Strait 20 ft,side passage to the left, then an aditional 10ft ahead
Down one level to Chamber
Rectangle 20x30 ft
1 Exit
wall opposite entrance
Door
Wooden
Passage extending 20 ft strait ahead
Continue Strait 30 ft,no doors or side passages
Continue Strait 30 ft,no doors or side passages
Continue Strait 20 ft,door to the left, then an aditional 10ft ahead
Continue Strait 20 ft,side passage to the right, then an aditional 10ft ahead
Down one level to Chamber
Rectangle 20x30 ft
1 Exit
wall opposite entrance
Door
Wooden
Passage extending 20 ft strait ahead

但是更大。但是我明白了

Continue Strait 20 ft,side passage to the right, then an aditional 10ft ahead
Down one level to Chamber
square 40x40 ft
1 Exit
wall opposite entrance
Door
Wooden
Chamber
Rectangle 20x30 ft
1 Exit
wall opposite entrance
Door

    at ReturnTopassagegenerator.main(ReturnTopassagegenerator.java:5)
    at RandomPassageGenerator.main(RandomPassageGenerator.java:43)
    at ReturnTopassagegenerator.main(ReturnTopassagegenerator.java:5)
    at RandomPassageGenerator.main(RandomPassageGenerator.java:16)
    at ReturnTopassagegenerator.main(ReturnTopassagegenerator.java:5)
    at RandomPassageGenerator.main(RandomPassageGenerator.java:20)
    at ReturnTopassagegenerator.main(ReturnTopassagegenerator.java:5)
    at RandomPassageGenerator.main(RandomPassageGenerator.java:12) 

还有这个

Passage extending 20 ft strait ahead
    Continue Strait 30 ft,no doors or side passages
    Continue Strait 30 ft,no doors or side passages
    Continue Strait 20 ft,door to the left, then an aditional 10ft ahead
    Continue Strait 30 ft,no doors or side passages
    Continue Strait 20 ft; passage ends in a doojava.lang.StackOverflowError
        at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:691)
        at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
        at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
        at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
        at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
        at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
        at java.io.PrintStream.write(PrintStream.java:526)
        at java.io.PrintStream.print(PrintStream.java:669)

我完全不知道该怎么做

谢谢

0 个答案:

没有答案