单击随机按钮时如何显示相同位置的不同字符串?

How to Show Different Strings With The Same Position When Clicking Random Button?

如何在单击随机按钮时显示具有相同位置的不同字符串,因此当字符串被随机化时,word 和 word2 在不同的文本视图中显示相同的位置数据。这几天一直在找资料,还没找到答案。我很困惑这样做你能帮我吗?这是我的 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();

}
}

如果你想 return 相同大小的不同数组中的相同位置,

试试这个

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