简体   繁体   English

单击按钮时 Java 中不重复单词 - Android App

[英]Not Repeating Words in Java when clicking button - Android App

I'm trying to create an android app that randomizes fruits but does not repeat them.我正在尝试创建一个 android 应用程序来随机化水果但不重复它们。 Something fun for my kids to do since we're all at home right now.因为我们现在都在家,所以我的孩子们可以做一些有趣的事情。 However, I'm still very new at programming and had some questions I was hoping you Pro's can help answer.但是,我对编程还是很陌生,有一些问题希望各位专业人士能帮助解答。

Here I go:这里我 go:

How can I not repeat the array when I click on the button in Java?单击Java中的按钮时如何不重复数组? I'm trying to generate fruits without them repeating.我试图在不重复的情况下产生水果。 Can I sort the string that way it runs through all the fruits one by one?我可以对字符串进行排序,使其一一遍历所有水果吗? It doesn't have to be randomized.它不必是随机的。 I just want each word to show only once when I click the button and show the last array "There aren't any fruit options left"当我单击按钮并显示最后一个数组“没有任何水果选项剩下”时,我只希望每个单词只显示一次

I tried to randomize the string but that repeats the fruits.我试图随机化字符串,但会重复结果。 I just want it to go one by one.我只想要go 一个一个。 When I press the button on my screen the output on the image label should give me each fruit one at a time.当我按下屏幕上的按钮时,图像 label 上的 output 应该一次给我一个水果。

ie. IE。 Button pressed" Output: "Apple"按下按钮” Output:“苹果”

button pressed again Output: "Banana"再次按下按钮 Output:“香蕉”

and so on until the last string shows "There aren't any fruit options left"依此类推,直到最后一个字符串显示“没有任何水果选项剩下”

Can someone also help me go back to the previous array?有人也可以帮我 go 回到以前的数组吗? For example if I clicked the buttons and Apple, Banana, Orange showed but I wanted to go back to see banana.例如,如果我单击按钮并显示 Apple、Banana、Orange 但我想 go 回来查看香蕉。 How would I do that?我该怎么做?

Thank you for your help!谢谢您的帮助!

public class Fruit extends AppCompatActivity {

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

        final View.OnClickListener myFamQ = new View.OnClickListener() {
            public void onClick(View v) {


                final TextView Fruittext = (TextView) findViewById(R.id.fruitFamilyText);
                final Button next = (Button) findViewById(R.id.nextQuestion);


                final String[] Fruit = new String [] {"Apple", "Banana", "Orange", "Cherry", "Grapes"};
                final int nextQ = 0;

                if (nextQ < Fruittext.length) {
                    Fruittext.setText(Fruittext[nextQ]);

                } else familyText.setText("There is no more left");



            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }

If you edit your code to this it should work without repeating your array.如果您将代码编辑为此,它应该可以在不重复数组的情况下工作。

public class Fruit extends AppCompatActivity {

Button next;
TextView Fruittext;

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

        next = findViewByID(R.id.nextQuestion);
        String[] fruit = new String [] {"Apple", "Banana", "Orange", "Cherry", "Grapes"};

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int nextQ = 0;

                if (nextQ < Fruittext.length) {
                    Fruittext.setText(fruit[nextQ]);
                    nextQ++;
                } else {
                    Fruittext.setText("There is no more available");
                }
            }
        });

If you want to show up a random Fruit in the TextView, just let me know, I'll help you!如果您想在 TextView 中出现随机水果,请告诉我,我会帮助您!

You could use an ArrayList and shuffle that easier, but you want to move the logic outside of your OnClickListener .您可以使用ArrayList并更轻松地洗牌,但您想将逻辑移到OnClickListener之外。

final String[] Fruit = new String [] {"Apple", "Banana", "Orange", "Cherry", "Grapes"};

// Create a new ArrayList to store the Fruits
final ArrayList<String> list = new ArrayList<String>();
// Add the entire Fruit Array to the list
Collections.addAll(list, Fruit);

// Shuffle the ArrayList to randomize the order
Collections.shuffle(list);
// Our index to keep track of what fruit we want next.
int index = 0;

final View.OnClickListener myFamQ = new View.OnClickListener() {
    public void onClick(View v) {
        final TextView Fruittext = (TextView) findViewById(R.id.fruitFamilyText);
        final Button next = (Button) findViewById(R.id.nextQuestion);

        // You can use the list's size as a max, and just get the next item each click.
        if (index < list.size()) {
            // Grab the next fruit and set the TextView.
            String fruit = list.get(index);
            Fruittext.setText(fruit );

            // Increase our index by 1.
            index = index + 1;
        } else {
            familyText.setText("There is no more left");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM