更新循环中的值

时间:2015-12-14 23:10:55

标签: java

我在下面的代码中更新error时遇到问题。我在下面列出了2个班级; MadLib类和MadLibRunner类。

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import static java.lang.System.*;
public class MadLib
{
    ArrayList<String> nouns;
    ArrayList<String> adjectives;
    ArrayList<String> verbs;
    public MadLib()
    {
        nouns = new ArrayList<String>();
        adjectives = new ArrayList<String>();
        verbs = new ArrayList<String>();
        nouns.add("dog");
        nouns.add("pig");
        nouns.add("chicken");
        nouns.add("building");
        nouns.add("car");
        nouns.add("person");
        nouns.add("place");
        nouns.add("thing");
        nouns.add("truck");
        nouns.add("city");
        nouns.add("state");
        nouns.add("school");
        nouns.add("student");
        nouns.add("bird");
        nouns.add("turkey");
        nouns.add("lion");
        nouns.add("tiger");
        nouns.add("alligator");
        nouns.add("elephant");
        adjectives.add("blue");
        adjectives.add("green");
        adjectives.add("orange");
        adjectives.add("fat");
        adjectives.add("skinny");
        adjectives.add("tall");
        adjectives.add("funny");
        adjectives.add("mad");
        adjectives.add("glad");
        adjectives.add("happy");
        adjectives.add("silly");
        adjectives.add("purple");
        adjectives.add("big");
        adjectives.add("little");
        adjectives.add("tiny");
        adjectives.add("huge");
        verbs.add("run");
        verbs.add("fly");
        verbs.add("skip");
        verbs.add("climb");
        verbs.add("clean");
        verbs.add("smell");
        verbs.add("eat");
        verbs.add("cry");
        verbs.add("smile");
        verbs.add("laugh");
        verbs.add("jump");
        verbs.add("crank");
        verbs.add("program");
    }
    public String sentence (String statement)
    {
        String output = statement;
        String word = "";
        Random rand = new Random();
        String error = "";
        for (int i = 0; i < output.length() - 1; i++)
        {
            if (nouns.size() > 0 && adjectives.size() > 0 && verbs.size() > 0)
            {
                if (output.charAt(i) == '#')
                {
                    int random = rand.nextInt(nouns.size());
                    word = nouns.get(random);
                    nouns.remove(random);
                    output = output.substring(0, i) + word + (output.substring(i + 1));
                }
                else if (output.charAt(i) == '&')
                {
                    int random = rand.nextInt(adjectives.size());
                    word = adjectives.get(random);
                    adjectives.remove(random);
                    output = output.substring(0, i) + word + (output.substring(i + 1));
                }
                else if (output.charAt(i) == '@')
                {
                    int random = rand.nextInt(verbs.size());
                    word = verbs.get(random);
                    verbs.remove(random);
                    output = output.substring(0, i) + word + (output.substring(i + 1));
                }
            }
            else
            {
                int nounCount = nouns.size() - 1;
                int adjectiveCount = adjectives.size() - 1;
                int verbCount = verbs.size() - 1;
                if (nounCount == 0)
                {
                    error = error + "Error: Not enough nouns.";
                }
                else if (adjectiveCount == 0)
                {
                    error = error + "Error: Not enough adjectives.";
                }
                else if (verbCount == 0)
                {
                    error = error + "Error: Not enough verbs.";
                }
                else if (nounCount == 0 && adjectiveCount == 0)
                {
                    error = error + "Error: Not enough nouns and adjectives.";
                }
                else if (nounCount == 0 && verbCount == 0)
                {
                    error = error + "Error: Not enough nouns and verbs.";
                }
                else if (adjectiveCount == 0 && verbCount == 0)
                {
                    error = error + "Error: Not enough adjectives and verbs.";
                }
                else if (nounCount == 0 && adjectiveCount == 0 && verbCount == 0)
                {
                    error = error + "Error: Not enough nouns, adjectives, and verbs.";
                }
                output = "Error: Not enough " + error;
                break;
            }
        }
        return output;
    }
}

跑步者......

public class MadLibRunner
{
    public static void main( String args[] )
    {
        boolean quit=false;
        MadLib prog = new MadLib();
        do
        {
            out.println("Welcome to MadLibs!\n Please input your coded sentence, or end to exit: ");
            Scanner in = new Scanner (System.in);
            String statement = in.nextLine();
            if(statement.equals("end"))
                quit=true;
            else
                out.println(prog.sentence(statement));
        }while(!quit);
        out.println("\nThank you, goodbye.\n");
    }
}

循环中else语句的目的是指定缺少哪些词性。我一直遇到的问题是errorfor的{​​{1}}循环结束时无法更新。我尝试更改MadLib的初始值来测试输出,它确实返回了error的测试值。如何让error在循环中更新?

哇,我刚才意识到我忘了澄清一些非常重要的东西。我应该能够在代码中输入多个句子,并且每次从列表中删除使用的名词/形容词/动词。如果在任何迭代期间对于整个句子的任何这些词性都不够,那么它应该显示错误消息。错误信息是什么不起作用。

2 个答案:

答案 0 :(得分:0)

构建MadLib时,你会添加名词,动词和形容词。因此,永远不会执行更新error的代码。

更确切地说,以下表达式始终求值为true:

if (nouns.size() > 0 && adjectives.size() > 0 && verbs.size() > 0)

因此,永远不会输入可能设置else的{​​{1}}块。

答案 1 :(得分:0)

这段代码非常详细,如果您决定要使用副词或其他内容,则会出现问题。

if (nounCount == 0)
{
    error = error + "Error: Not enough nouns.";
}
else if (adjectiveCount == 0)
{
    error = error + "Error: Not enough adjectives.";
}
else if (verbCount == 0)
{
    error = error + "Error: Not enough verbs.";
}
else if (nounCount == 0 && adjectiveCount == 0)
{
    error = error + "Error: Not enough nouns and adjectives.";
}
else if (nounCount == 0 && verbCount == 0)
{
    error = error + "Error: Not enough nouns and verbs.";
}
else if (adjectiveCount == 0 && verbCount == 0)
{
    error = error + "Error: Not enough adjectives and verbs.";
}
else if (nounCount == 0 && adjectiveCount == 0 && verbCount == 0)
{
    error = error + "Error: Not enough nouns, adjectives, and verbs.";
}
output = "Error: Not enough " + error;
break;

我会对事情进行重组,以便您使用替换代码进行检查。

String replacement;

if (output.charAt(i) == '#')
{
    if (nouns.size() > 0)
    {
        int random = rand.nextInt(nouns.size());
        replacement = nouns.get(random);
        nouns.remove(random);
    } else {
        return "Error: ran out of nouns";
    }
} else if  ...

output = output.substring(0, i) + replacement+ (output.substring(i + 1));

这并不完美,但通过这种方式,您可以最大限度地减少特定错误案例的数量。另外,如果句子中没有形容词替换,你不关心是否没有形容词。

在它的当前形式中,一旦你取消- 1,你的代码应该可以工作,因为大小是基于一个。如果列表中没有项目,则大小将为0,因此您的案例将为if (nouns.size() == 0)