Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 陷入递归循环_Java_String_Recursion - Fatal编程技术网

Java 陷入递归循环

Java 陷入递归循环,java,string,recursion,Java,String,Recursion,我正在做一个课堂作业,我必须使用递归代码从列表中提取不同的单词,并形成句子。在大多数情况下,我的代码的这一部分是有效的,但问题是,它不会停止。我将在下面发布我的代码,任何提示或解决方案都将不胜感激 /* * A program that implements BNF rules to generate random sentences. * * The program generates and outputs one random sentence every three * sec

我正在做一个课堂作业,我必须使用递归代码从列表中提取不同的单词,并形成句子。在大多数情况下,我的代码的这一部分是有效的,但问题是,它不会停止。我将在下面发布我的代码,任何提示或解决方案都将不胜感激

/*
 * A program that implements BNF rules to generate random sentences.
 * 
 * The program generates and outputs one random sentence every three
 * seconds until it is halted (for example, by typing Control-C in the
 * terminal window where it is running).
 * 
 * The rules that this program follows are:
 * 
 * <sentence> ::= <simple_sentence> [ <conjunction> <sentence> ]
 * 
 * <simple_sentence> ::= this is a <adjective> project <conjunction> 
 * <noun_phrase> <verb_phrase>
 * 
 * <noun_phrase> ::= <proper_noun> | 
 * <determiner> [ <adjective> ]. <common_noun> [ who <verb_phrase> ]
 * 
 * <verb_phrase> ::= <intransitive_verb> | 
 * <transitive_verb> <noun_phrase> |
 * is <adjective> |
 * believes that <simple_sentence>
 * 
 * Rules for nouns, verbs, conjunctions, etc. are implemented by arrays.
 */
package randomrecursion;

import java.util.Random;

/**
 *
 * @author Xavier
 */

    public class RandomRecursion {

    // Implement arrays for last seven rules in the list.
    private static final String[] conjunctions = { "and", "or", "but", 
        "because", "since", "when" };

    private static final String[] properNouns = { "Fred", "Jane", 
        "Richard Nixon", "Miss America", "Berry White", "Jeff Dunham" };

    private static final String[] commonNouns = { "man", "woman", "fish",
        "elephant", "ball", "planet", "model", "stunt-man" };

    private static final String[] determiners = { "a", "the", "every", "some",
        "each", "neither", "either" };

    private static final String[] adjectives = { "big", "tiny", "pretty",
        "bald", "odd", "scary", "unfortunate", "nice", "rediculus", "carefree" };

    private static final String[] intransitiveVerbs = { "runs", "jumps",
        "talks", "sleeps", "eats", "cries", "laughs" };

    private static final String[] transitiveVerbs = { "loves", "hates", "sees",
        "knows", "lives", "hunts", "accepts", "cleans" };

    // Instantiate a Random() object.
    private static final Random rGen = new Random();

    /* 
     * rGen will be used by chanceToBeTrue to select a double in the
     * range 0-1.  If the value is less than SMALL_CHANCE,
     * chanceToBeTrue(SMALL_CHANCE) returns true.
     * MEDIUM_CHANCE and LARGE_CHANCE work in the same way.
     * 0.1 is equivalent to a 10% chance.
     */
    private static final double SMALL_CHANCE = 0.1;
    private static final double MEDIUM_CHANCE = 0.5;
    private static final double LARGE_CHANCE = 0.75;

    /*
     * Values will be added together and used with if/else statements
     * to determine probabilities for branches in the verbPhrase()
     * routine.  0.1 is equivalent to a 10% chance.
     */
    private static final double CHANCE_FOR_INTRANSITIVE = 0.4;
    private static final double CHANCE_FOR_TRANSITIVE = 0.1;
    private static final double CHANCE_FOR_ADJECTIVE = 0.4;

    private static final int MILLISECONDS_TO_SLEEP = 2000;

    /**
     * This routine calls the randomSentence routine to generate random 
     * sentences in a loop, pausing for MILLISECONDS_TO_SLEEP in
     * between each sentence.
     * 
     * @param args Command line args (not used).
     */
    public static void main(String[] args) {

        while (true) {
            randomSentence();
            System.out.println(".\n\n");
            try {
                Thread.sleep(MILLISECONDS_TO_SLEEP);
            } catch (InterruptedException e) {

            }
        }
    }

    /**
     * This routine creates one random sentence following this rule:
     * 
     * <sentence> ::= <simple_sentence> [ <conjunction> <sentence> ]
     */
    private static void randomSentence() {

        simpleSentence();

        if (chanceToBeTrue(SMALL_CHANCE)) {
            randomItem(conjunctions);
            System.out.print(" ");
            randomSentence();
        }
    }

    /**
     * This routine creates a simple sentence following this rule:
     * 
     * <simple_sentence> ::= this is a <adjective> project <conjunction>
     * <noun_phrase> <verb_phrase>
     */
    private static void simpleSentence() {

        makeItInteresting();
        nounPhrase();
        verbPhrase();
    }

    /**
     * This routine adds an interesting beginning to each simpleSentence.
     */
    private static void makeItInteresting() {
        System.out.print("this is a");
        randomItem(adjectives);
        System.out.print(" project");
        randomItem(conjunctions);
    }

    /**
     * This routine creates a noun phrase following this rule:
     * 
     * <noun_phrase> ::= <proper_noun> |
     * <determiner> [ <adjective> ] <common_noun> [ who <verb_phrase> ]
     */
    private static void nounPhrase() {

        if (chanceToBeTrue(MEDIUM_CHANCE)) {
            randomItem(properNouns);
        } else {
            randomItem(determiners);
            if (chanceToBeTrue(LARGE_CHANCE)) {
                randomItem(adjectives);
            }
            randomItem(commonNouns);
            if (chanceToBeTrue(SMALL_CHANCE)) {
                System.out.print(" who");
                verbPhrase();
            }
        }
    }

    /**
     * This routine creates a verb phrase following this rule:
     * 
     * <verb_phrase> ::= <intransitive_verb> |
     * <transitive_verb> <noun_phrase> |
     * is <adjective> |
     * believes that <simple_sentence>
     */
    private static void verbPhrase() {

        // Instantiate a random double between 0.0 (inclusive)
        // and 0.1 (exclusive).
        double chance = rGen.nextDouble();

        if (chance < CHANCE_FOR_INTRANSITIVE) {
            randomItem(intransitiveVerbs);
        } else if (chance < (CHANCE_FOR_INTRANSITIVE + CHANCE_FOR_TRANSITIVE)) {
            randomItem(transitiveVerbs);
            nounPhrase();
        } else if (chance < (CHANCE_FOR_INTRANSITIVE
                             + CHANCE_FOR_TRANSITIVE + CHANCE_FOR_ADJECTIVE)) {
            System.out.print(" is");
            randomItem(adjectives);
        } else {
            System.out.print(" believes that ");
            simpleSentence();
        }
    }    

    /**
     * This routine randomly chooses an item from an array of strings.    
     */
    private static void randomItem(String[] listOfStrings) {

        /* 
         * Get next integer from rGen that is between 0 (inclusive) and 
         * listOfStrings.length (exclusive) and print that randomly chosen
         * element of the list of Strings.
         */
        int choice = rGen.nextInt(listOfStrings.length);
        System.out.print(" " + listOfStrings[choice]);
    }


    /**
     * This routine uses rGen to return true percentChance amount
     * of the time.
     * 
     * @param percentChance double value between 0.0 (inclusive)
     *     and 1.0 (exclusive).
     * @return true if a randomly chosen double value
     *     is less than percentChance.
     */
    private static boolean chanceToBeTrue(double percentChance) {

        double number = rGen.nextDouble();
        if (number < percentChance) {
            return true;
        } else {
            return false;
        }
    }
}
/*
*实现BNF规则以生成随机句子的程序。
* 
*程序每三年生成并输出一个随机句子
*秒,直到停止(例如,通过在
*正在运行的终端窗口)。
* 
*本程序遵循的规则如下:
* 
*  ::=  [   ]
* 
*这是一个项目
*  
* 
*  ::=  | 
*  [  ].  [世卫组织]
* 
*  ::=  | 
*   |
*是|
*相信
* 
*名词、动词、连词等的规则由数组实现。
*/
包随机递归;
导入java.util.Random;
/**
*
*@作者Xavier
*/
公共类随机递归{
//实现列表中最后七条规则的数组。
私有静态最终字符串[]连接={“and”,“or”,“but”,
“因为”、“因为”、“什么时候”};
私有静态最终字符串[]properNouns={“Fred”,“Jane”,
“理查德·尼克松”、“美国小姐”、“贝里·怀特”、“杰夫·邓纳姆”};
私有静态最终字符串[]commonnomes={“男人”、“女人”、“鱼”,
“大象”、“球”、“行星”、“模特”、“特技演员”};
私有静态最终字符串[]限定符={“a”、“the”、“every”、“some”,
“每个”、“都没有”、“或者”};
私有静态最终字符串[]形容词={“大”、“小”、“漂亮”,
“秃头”、“古怪”、“可怕”、“不幸”、“美好”、“放纵”、“无忧无虑”};
私有静态最终字符串[]不及物动词={“运行”、“跳跃”,
“说话”、“睡觉”、“吃饭”、“哭”、“笑”};
私有静态最终字符串[]传递动词={“爱”、“恨”、“看”,
“知道”、“生活”、“狩猎”、“接受”、“清洁”};
//实例化一个Random()对象。
私有静态最终随机rGen=新随机();
/* 
*chanceToBeTrue将使用rGen在
*范围0-1。如果该值小于小概率,
*chanceToBeTrue(小概率)返回true。
*中等机会和大机会的工作方式相同。
*0.1相当于10%的几率。
*/
私人静态最终双小概率=0.1;
私人静态最终双中奖机会=0.5;
私人静态最终双大_机会=0.75;
/*
*值将被添加在一起,并与if/else语句一起使用
*确定VerbPhase()中分支的概率
*常规。0.1相当于10%的几率。
*/
不及物性的私有静态最终双机会=0.4;
传递的私有静态最终双机会=0.1;
形容词的私有静态最终双概率=0.4;
私有静态最终整数毫秒到睡眠=2000;
/**
*此例程调用随机语句例程以生成随机语句
*句子在循环中,停顿几毫秒,直到睡着
*在每句话之间。
* 
*@param args命令行args(未使用)。
*/
公共静态void main(字符串[]args){
while(true){
随机句子();
System.out.println(“.\n\n”);
试一试{
睡眠(毫秒到睡眠);
}捕捉(中断异常e){
}
}
}
/**
*此例程根据此规则创建一个随机句子:
* 
*  ::=  [   ]
*/
私有静态无效语句(){
simpleSentence();
如果(偶然发现真的(小概率)){
随机项目(连词);
系统输出打印(“”);
随机句子();
}
}
/**
*此例程根据以下规则创建一个简单的句子:
* 
*这是一个项目
*  
*/
私有静态void simpleContent(){
使之有趣();
名词短语();
动词短语();
}
/**
*此例程为每个SimpleContent添加了一个有趣的开始。
*/
私有静态void MakeitInterest(){
系统输出打印(“这是一个”);
随机项(形容词);
系统输出打印(“项目”);
随机项目(连词);
}
/**
*此例程根据以下规则创建名词短语:
* 
*  ::=  |
*[]世卫组织]
*/
私有静态空名词短语(){
如果(偶然发现真实(中等概率)){
随机项目(适当盎司);
}否则{
随机项目(决定因素);
如果(偶然发现真实(大概率)){
随机项(形容词);
}
随机项(普通名词);
如果(偶然发现真的(小概率)){
系统输出打印(“who”);
动词短语();
}
}
}
/**
*此例程根据以下规则创建动词短语:
* 
*  ::=  |
*   |
*是|
*相信
*/
私有静态void verbPhase(){
//实例化0.0(含0.0)之间的随机双精度
//和0.1(不含)。
双机会=rGen.nextDouble();
if(chancewhile (true) {
    randomSentence();
    System.out.println(".\n\n");
    try {
        Thread.sleep(MILLISECONDS_TO_SLEEP);
    } catch (InterruptedException e) {

    }
}