【问题标题】:How to Show Different Strings With The Same Position When Clicking Random Button?单击随机按钮时如何显示具有相同位置的不同字符串?
【发布时间】:2018-11-06 15:53:02
【问题描述】:

如何在单击随机按钮时显示具有相同位置的不同字符串,因此当字符串被随机化时,word 和 word2 显示相同的位置数据并在不同的 textview 中。这几天一直在找资料,还没找到答案。我很困惑这样做你能帮我吗?这是我的 Java 文件。

Adapter.java

package com.word.game;
import java.util.Random;
public class Adapter {

public static final Random RANDOM = new Random();
public static final String[] WORDS = {"RUN",
"WALK",
"CRY",
"SUGAR",
"SALT",
"RAIN",
"NOVEMBER"};

public static final String[] WORDS2 = {"FAST",
"RELAX",
"TEARS",
"DRINK",
"RECIPERS",
"WATER",
"CALENDAR"};


public static String randomWord2() {
    return WORDS2[RANDOM.nextInt(WORDS2.length)];
}

public static String randomWord() {
    return WORDS[RANDOM.nextInt(WORDS.length)];
}

public static String shuffleWord(String word) {
    if (word != null  &&  !"".equals(word)) {
        char a[] = word.toCharArray();

        for (int i = 0; i < a.length; i++) {
            int j = RANDOM.nextInt(a.length);
            char tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }

        return new String(a);
    }

    return word;
}

}

MainActivity.java

package com.papaozi.pilgub;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements         View.OnClickListener{
private MediaPlayer mediaPlayerbg, mediaPlayerwin, mediaPlayerSalah;
private TextView suffletext, quest;
private EditText answer;
private Button validate, newGame;
private String wordToFind, wordToFind2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);
    quest = (TextView) findViewById(R.id.quest);
    suffletext = (TextView) findViewById(R.id.suffletext);
    answer = (EditText) findViewById(R.id.wordEnteredEt);
    validate = (Button) findViewById(R.id.validate);
    validate.setOnClickListener(this);
    newGame = (Button) findViewById(R.id.newGame);
    newGame.setOnClickListener(this);
    answer.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
    newGame();
    initViews();
}
private void initViews() {

    mediaPlayerbg = MediaPlayer.create(this, R.raw.spooky);
    mediaPlayerbg.start();
    mediaPlayerbg.setLooping(true);
}
@Override
public void onClick(View view) {
    if (view == validate) {
        validate();
    } else if (view == newGame) {
        newGame();
    }
}

private void validate() {
    String w = answer.getText().toString();
    Context context = getApplicationContext();
    LayoutInflater inflater = getLayoutInflater();
    View customToastroot = inflater.inflate(R.layout.custom_toast_benar, null);
    final Toast customtoast = new Toast(context);
    customtoast.setView(customToastroot);
    customtoast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 0);
    customtoast.setDuration(Toast.LENGTH_LONG);

    Context context2 = getApplicationContext();
    LayoutInflater inflater2 = getLayoutInflater();
    View customToastroot2 = inflater2.inflate(R.layout.custom_toast_salah, null);
    final Toast customtoast2 = new Toast(context2);
    customtoast2.setView(customToastroot2);
    customtoast2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 0);
    customtoast2.setDuration(Toast.LENGTH_LONG);

    if (wordToFind.equals(w)) {
        customtoast.show();
        newGame();
        mediaPlayerwin = MediaPlayer.create(this, R.raw.winner);
        mediaPlayerwin.start();
    } else {
        customtoast2.show();
        mediaPlayerSalah = MediaPlayer.create(this, R.raw.draw);
        mediaPlayerSalah.start();
    }
}

private void newGame() {
    wordToFind = Adapter.randomWord();
    String wordShuffled = Adapter.shuffleWord(wordToFind);
    suffletext.setText(wordShuffled);
    answer.setText("");;
}
protected void onDestroy() {
    super.onDestroy();
    if (mediaPlayerbg != null) {
        mediaPlayerbg.release();
        mediaPlayerbg = null;
    }
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(MainActivity.this, SecondActivity.class));
    mediaPlayerbg.stop();
    finish();

}
}

【问题讨论】:

  • 你使用了两次WORDS变量名!!!请清楚地编辑您的问题
  • 感谢您的关注,抱歉我太累了。 :d:d

标签: java android anagram


【解决方案1】:

如果你想在不同的数组中以相同的大小返回相同的位置,

试试这个

public static String randomWord2(int pos) {
    return WORDS2[pos];
}

public static String randomWord(int pos) {
    return WORDS[pos];
}

public static int randomNum() {
    return RANDOM.nextInt(WORDS.length);
}

当您调用 randomWord()randomWord2() 时,首先获取随机位置并将其传递给方法

int pos= randomNum();
word1=randomWord(pos);
word2=randomWord2(pos);

【讨论】:

  • @OnadHanif 我希望两个数组的大小相同,您可以尝试更新后的答案
  • 如果 mainactivity 是我更新的问题,我该如何实现?
  • @OnadHanif 您根本没有使用 randomWord2()。那为什么两个数组都需要相同的位置。你能解释一下你到底想要什么吗?
  • 对不起,我不小心,现在非常感谢。
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多