我收到的代码无法解决符号错误

时间:2016-07-08 23:30:11

标签: android

问题出在这个代码块中,带有“quote”一词

if (quotesCopy.size() > 20) {
    quotesCopy = new ArrayList<String>();
}

displayMessageTextView.setText(quote);

这是我的所有代码:

public class MainActivity extends AppCompatActivity {

    private Button addButton;
    private TextView displayMessageTextView;
    private ArrayList<String> quotesCopy = new ArrayList<String>();
    private ArrayList<String> quotes = new ArrayList<String>();
    private Random randomGenerator = new Random();
    private int previousNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        quotes.add("You smart! You loyal! You're a genious!");
        quotes.add("I appreciate you");
        quotes.add("This is a major key");
        quotes.add("They will try to close the door on U, just open it");
        quotes.add("The key to more success is CoCo butter");
        quotes.add("Congratulations, you played yourself");
        quotes.add("Another one, no. Another two");
        quotes.add("I changed... a lot");
        quotes.add("They don't want you to jet ski");
        quotes.add("The key is to have every key");
        quotes.add("Almond milk + Cinnamon Toast Crunch = Major key to success");
        quotes.add("Do you see that bamboo? Ain't nothing like bamboo. Blessup");
        quotes.add("Bless up. Egg whites, Turkey bacon, Hashbrown, Water");
        quotes.add("They wanna come stress me out? Heh, bye");
        quotes.add("Lion Order");
        quotes.add("Watch your back, but when you get out of the shower dry your back, it's a cold world");
        quotes.add("Some of ya'll crabs");
        quotes.add("ANOTHA ONE");
        quotes.add("We jus seen 2 dolphins");
        quotes.add("They don't want you to win");
        quotes.add("Be A Star. Be A Superstar");
        quotes.add("I remember when I ain't have a jacuzzi");
        quotes.add("The other day the grass was brown, now it's green cuz I ain't give up");
        quotes.add("In life everyone has a choice. The key is...make a right choice");
        quotes.add("We have to get money. We have no choice. It cost money to eat");
        quotes.add("I love my bamboo trees. I love fruits. I love apples.");
        quotes.add("I told y'all this before, when you have a swimming pool do not use chlorine, use salt water");
        quotes.add("The key is: never fold");
        quotes.add("major key, get a pedicure and manicure once a week");
        quotes.add("They dont want you to be healthy");
        quotes.add("To make it thru the jungle you're gonna have to sweat");
        quotes.add("They never said winning was easy");
        quotes.add("It's important to shape up your hedges. It's like getting a haircut");
        quotes.add("LIOOOOON");
        quotes.add("To succeed, you must believe. When you believe you will succeed. ");
        quotes.add("The key to success is to have a hammock");
        quotes.add("Some people can't handle winning. I can.");
        quotes.add("They don't want you to have lunch");
        quotes.add("It's not an easy road but give thanks to the road");
        quotes.add("The key to success is to have a lot of pillows. A lot.");

        displayMessageTextView = (TextView) findViewById(R.id.displayMessageTextView);
        displayMessageTextView.setText("Press the button to generate a random quote");

        addButton = (Button) findViewById(R.id.addObjecttive);
        addButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Boolean isDifferent = false;
                while (!isDifferent) {
                    int randomQuoteIndex = getRandomNumber();
                    String quote = quotes.get(randomQuoteIndex);
                    if (!quotesCopy.contains(quote)) {
                        isDifferent = true;
                        quotesCopy.add(quote);
                    }
                    if (quotesCopy.size() > 20)
                        quotesCopy = new ArrayList<String>();
                }

                displayMessageTextView.setText(quote);
            }
        });
    }

    public int getRandomNumber() {
        int randomNumber = randomGenerator.nextInt(40);

        while (previousNumber == randomNumber) {
            randomNumber = randomGenerator.nextInt(40);
        }

        previousNumber = randomNumber;

        return randomNumber;
    }
}

1 个答案:

答案 0 :(得分:4)

您需要在while循环外声明quote以在循环

之外引用它
@Override
    public void onClick(View v) {
        Boolean isDifferent = false;
        String quote ;
        while (!isDifferent) {
            int randomQuoteIndex = getRandomNumber();
            quote = quotes.get(randomQuoteIndex);
            if (!quotesCopy.contains(quote)) {
                isDifferent = true;
                quotesCopy.add(quote);
            }
            if (quotesCopy.size() > 20)
                quotesCopy = new ArrayList<String>();
        }

        displayMessageTextView.setText(quote);
    }
相关问题