0

这是一个生成随机字符串的类文件。

public class RandomGen {
    static String keys() {
        String str[] = new String[25];
        str[0] = "Press A1";
        str[1] = "Press A2";
        str[2] = "Press A3";
        str[3] = "Press A4";
        str[4] = "Press A5";
        str[5] = "Press B1";
        str[6] = "Press B2";
        str[7] = "Press B3";
        str[8] = "Press B4";
        str[9] = "Press B5";
        str[10] = "Press C1";
        str[11] = "Press C2";
        str[12] = "Press C3";
        str[13] = "Press C4";
        str[14] = "Press C5";
        str[15] = "Press D1";
        str[16] = "Press D2";
        str[17] = "Press D3";
        str[18] = "Press D4";
        str[19] = "Press D5";
        str[20] = "Press E1";
        str[21] = "Press E2";
        str[22] = "Press E3";
        str[23] = "Press E4";
        str[24] = "Press E5";

        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(25);
        return str[randomInt];
    }
}

这是安卓活动文件。我从一个类中返回一个随机文本并在文本视图中显示。如果返回的字符串是“Press A1”,用户应该只按下 Al 按钮。如果他按下其他按钮,则必须在其他文本视图中显示错误消息。下次如果该文本视图中显示“Press C1”,用户应该只按下 C1 按钮。如果他按下其他按钮,则必须显示“错误按钮”消息。我不知道如何检查条件。请帮我。

    RandomGen rg = new RandomGen();
    String s;
    b[0].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A1 is pressed");
        }
    });
    b[1].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A2 is pressed");
        }
    });
    b[2].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A3 is pressed");

        }
    });
    b[3].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A4 is pressed");
        }
    });
4

2 回答 2

0

If you would want to test this I would propose to create a RandomGenerator interface with two implementations. One implementation which returns a predefined set of results and one which uses a Random to provide the answers.

This way the predefined RandomGenerator can be used during testing and the random RandomGenerator during the running of the application.

The code below demonstrates the setup I illustrated above. Pass a IntegerGenerator in your keys method and use it like this:

public static String keys(IntegerGenerator generator) {
    // code to create keys str
    int randomInt = generator.nextInt(str.length);
    return str[randomInt];

}

interface IntegerGenerator {
    public int nextInt(int maximum);
}

class PredefinedIntegerGenerator implements IntegerGenerator {

    private final int[] values;
    private int index;

    public PredefinedIntegerGenerator(int[] values) {
        this.values = values;
        this.index = 0;
    }

    @Override
    public int nextInt(int maximum) {
        if (index >= values.length) {
            index = 0;
        }
        return values[index];

    }
}

class RandomIntegerGenerator implements IntegerGenerator {

    private final Random random;

    public RandomIntegerGenerator(Random random) {
        this.random = random;
    }

    @Override
    public int nextInt(int maximum) {
        return random.nextInt(maximum);
    }
}
于 2012-07-19T10:03:02.233 回答
0

You have a whole lot of code duplication here. Here's what you should do, IMHO.

Create a static final array of texts, containing the text of all your buttons.

Create a Map<View, String> containing the text associated with each button (unless Android allows associateing some text to a button, in which case you don't even need this map).

For each button add the same listener:

View.OnCliskListener theListener = new View.OnClickListener() {
    public void onClick(View buttonClicked) {
        String textOfTheButtonClicked = map.get(buttonClicked);
        if (!textOfTheButtonClicked.equals(theTextChosenRandomly)) {
            // do what you want here
        }
    }
};
于 2012-07-19T10:13:04.093 回答