Ich versuche, zwei Zeichenfolgen zu verbinden, die nicht dasselbe Wort für übereinstimmendes Spiel haben. Ich habe den Code, den ich erstellt habe, mit demselben Text arbeiten. Wenn zum Beispiel die erste Auswahl = "Wort1" ist, muss die zweite Auswahl denselben Text "Wort1" haben. Ich möchte die erste Auswahl = "Wort1" und die zweite Auswahl = "Definition1". Bitte helfen Sie für weitere Details siehe den Code unten.Wie man zwei Zeichenfolgen verbindet, die nicht das gleiche Wort haben
protected void onCreate(Bundle savedInstanceState) {
text[0] = "word1";
text[1] = ("definition1");
twotext[0] = "word2";
twotext[1] = ("definition2");
threetext[0] = "word3";
threetext[1] = ("definition3");
foretext[0] = "word4";
foretext[1] = ("definition4");
}
public void setBoard()
{
board = new String[8];
//Assigns each image to two random positions
for(int i = 0; i < 2; i++)
{
int pic1 = (int)(Math.random()*board.length);
int pic2 = (int)(Math.random()*board.length);
int pic3 = (int)(Math.random()*board.length);
int pic4 = (int)(Math.random()*board.length);
while(board[pic1]!=null)
{
pic1 = (int)(Math.random()*board.length);
}
board[pic1] = text[i];
while(board[pic2]!=null)
{
pic2 = (int)(Math.random()*board.length);
}
board[pic2] = twotext[i];
while(board[pic3]!=null)
{
pic3 = (int)(Math.random()*board.length);
}
board[pic3] = threetext[i];
while(board[pic4]!=null)
{
pic4 = (int)(Math.random()*board.length);
}
board[pic4] = foretext[i];
}
//Sets the question mark image to each ImageView and makes each clickable
for(int i = 0; i < board.length; i++)
{
pics[i].setText(board[i]);
}
for(Button i: pics)
{
i.setClickable(true);
}
}
//If an image is clicked it will "flip" to reveal the image below
//If the first and second pick match, 100 points are awarded, otherwise 200 are deducted
public void click(View v){
switch (v.getId())
{
case R.id.i0:
pics[0].setText(board[0]);
if(firstPick) pick1 = 0;
else pick2 = 0;
break;
case R.id.i1:
pics[1].setText(board[1]);
if(firstPick) pick1 = 1;
else pick2 = 1;
break;
case R.id.i2:
pics[2].setText(board[2]);
if(firstPick) pick1 = 2;
else pick2 = 2;
break;
case R.id.i3:
pics[3].setText(board[3]);
if(firstPick) pick1 = 3;
else pick2 = 3;
break;
case R.id.i4:
pics[4].setText(board[4]);
if(firstPick) pick1 = 4;
else pick2 = 4;
break;
case R.id.i5:
pics[5].setText(board[5]);
if(firstPick) pick1 = 5;
else pick2 = 5;
break;
case R.id.i6:
pics[6].setText(board[6]);
if(firstPick) pick1 = 6;
else pick2 = 6;
break;
case R.id.i7:
pics[7].setText(board[7]);
if(firstPick) pick1 = 7;
else pick2 = 7;
break;
}
if(!firstPick)
{
pics[pick2].setClickable(false);
if(board[pick1]==board[pick2]) {
matches++;
score += 100;
layout.setBackgroundColor(Color.GREEN);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
layout.setBackgroundColor(Color.WHITE);
}
}, 500);
}
............ ............
Wenn es keine Duplizierung in Wörtern gibt, empfehle ich Ihnen, HashMap zu verwenden. Es hat - Format. Wenn Sie also erhalten möchten, geben Sie einfach ein. –
Kae10
"Übereinstimmen" mit smth zu smth bedeutet, dass von einer Menge injiziert wird, zu einer Menge <, zu der Sie passen> = es gibt keine Duplizierung. Sie können per Definition nicht zwei verschiedene Dinge "zusammenbringen". –
iantonuk
das Problem, dass ich in dieser Zeile bemerke "wenn (Board [pick1] == board [pick2])". Wenn ich die erste Wahl wähle, muss die gleiche sein. Ich brauche einen Weg, um das zweite Wort zu ändern. zum Beispiel "Satz" == "Wörter". In diesem Code muss ich das gleiche Wort in zwei wählen wie "Satz" == "Satz". danke für die Hilfe. –