简体   繁体   中英

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

How to Show Different Strings With The Same Position When Clicking Random Button, So when the string is randomized the word and word2 display the same position data and in different textview. I have been looking for information for a few days and have not found an answer yet. I am confused to do so can you help me? Here's the Java file I have.

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

}
}

If you want to return same position in different arrays with same size,

Try this

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

When You are invoking randomWord() and randomWord2() , get the random position first and the pass it to the methods

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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