【问题标题】:Not Repeating Words in Java when clicking button - Android App单击按钮时不会在 Java 中重复单词 - Android 应用程序
【发布时间】:2020-04-20 19:48:05
【问题描述】:

我正在尝试创建一个随机分配水果但不重复的 Android 应用。因为我们现在都在家,所以我的孩子们可以做一些有趣的事情。 不过,我还是个编程新手,有一些问题希望各位高手能帮忙解答。

我来了:

点击Java中的按钮时如何不重复数组?我试图在不重复的情况下产生水果。我可以对字符串进行排序,使其一一遍历所有水果吗?它不必是随机的。我只希望每个单词在单击按钮时只显示一次并显示最后一个数组“没有任何水果选项”

我试图随机化字符串,但结果是重复的。我只是希望它一个接一个。当我按下屏幕上的按钮时,图像标签上的输出应该一次给我一个水果。

即。按下按钮”输出:“Apple”

按钮再次按下输出:“香蕉”

以此类推,直到最后一个字符串显示“没有任何水果选项”

有人也可以帮我回到以前的数组吗?例如,如果我点击按钮,Apple、Banana、Orange 显示,但我想回去看看香蕉。我该怎么做?

感谢您的帮助!

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) {

                }

【问题讨论】:

    标签: java android mobile


    【解决方案1】:

    如果你编辑你的代码,它应该可以在不重复你的数组的情况下工作。

    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");
                    }
                }
            });
    

    如果您想在 TextView 中显示随机水果,请告诉我,我会帮助您!

    【讨论】:

    • 嗨!哇,我非常感谢您的帮助!太感谢了!如何在 TextView 中显示它??
    • 好吧,如果你想在 TextView 中显示一个随机水果,你应该像这样随机化 nextQ int:而不是 int nextQ = 0;你应该写 int nextQ = (int) (Math.random() * (fruit.length - 1)) 。也请删除nextQ++; .
    • 如果你现在想从数组中删除这个水果,只需替换 nextQ++;用这个:fruit = ArrayUtils.remove(test, nextQ);
    • 我试图这样做,但它没有显示在我的文本视图中。相反,应用程序正在关闭。
    • 你能把你的安卓工作室项目发给我吗?然后我会看看它并尝试修复错误。 (为此,请单击导航栏中的“文件”,然后单击“导出到 Zip 文件”)
    【解决方案2】:

    您可以使用 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");
            }
        }
    }
    

    【讨论】:

    • 谢谢!我在大括号 } 上遇到错误。这些金额正确吗?
    • 我可能遗漏了一个括号或添加了一个额外的括号。所有这些代码都应该放在你的 onCreate() 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多