我目前正在创建一个使用Android Studio 3.2的移动应用程序,其中包含FLAG猜测游戏。在其中一款游戏中,我必须显示一个随机标志和相应的名称(覆盖着破折号)。用户可以将字母输入到下面的编辑文本框中,然后单击"提交"按钮。如果用户得到正确的答案,则删除该信件的破折号以显示实际字母。
显示App的UI
的图像我的问题是从单独更换每个破折号开始。当我输入一封信并提交时,所有破折号都会变成同一信。
package com.example.anisa.assignment1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class GuessHints extends AppCompatActivity
{
private ImageView flag;
private int randIndex;
public char[] answers = {};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guess_hints);
displayHintsFlag();
splitCountryNameLetters();
}
public void displayHintsFlag()
{
flag = findViewById(R.id.displayHintsFlag);
Random r = new Random();
int min = 0;
int max = 255;
randIndex = r.nextInt(max-min) + min;
Country countries = new Country();
int randomHintsFlagImage = countries.countryImages[randIndex];
flag.setImageResource(randomHintsFlagImage);
}
public void splitCountryNameLetters()
{
Country countries = new Country();
String randomHintsFlagName = countries.countryNames[randIndex];
TextView hintsQuestion = (TextView) findViewById(R.id.countryDashesDisplay);
String hintsQuestionString;
int flagNameLength = randomHintsFlagName.length();
char letter;
for (int i = 0; i < flagNameLength; i++)
{
Log.d("Flag: ",randomHintsFlagName + "");
hintsQuestionString = hintsQuestion.getText().toString();
letter = '-';
hintsQuestion.setText(hintsQuestionString + " " + letter);
}
Log.d("Answers: ", answers + "");
}
public void checkUserEntries(View view)
{
Country countries = new Country();
String randomHintsFlagName = countries.countryNames[randIndex];
int flagNameLength = randomHintsFlagName.length();
TextView hintsQuestion = (TextView) findViewById(R.id.countryDashesDisplay);
String hintsQuestionString = hintsQuestion.getText().toString();
EditText userEntry = (EditText) findViewById(R.id.enterLetters);
String userEntryText = userEntry.getText().toString();
//int numCorr = 0;
char letterChar = userEntryText.charAt(0);
for(int k = 0; k < flagNameLength; k++)
{
if((letterChar == randomHintsFlagName.charAt(k)))
{
//numCorr++;
hintsQuestionString = hintsQuestionString.replace(hintsQuestionString.charAt(k), letterChar);
}
}
hintsQuestion.setText(hintsQuestionString);
}
public void nextGuessHints(View view)
{
Button submitButtonHints = (Button) findViewById(R.id.submitLetterButton);
submitButtonHints.setText("Next");
Intent intent = getIntent();
finish();
startActivity(intent);
}
@Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(GuessHints.this, MainActivity.class));
finish();
}
}
我知道问题是使用k索引,但不确定如何解决此问题,就像for循环中的问题一样。
说您有一个启动String
,例如Italia
。
用户输入字母i
,应该发生的是
------ > I---i-
让我们首先将要猜测的String
转换为版本
final String toBeGuessed = "Italia"; // Italia
final String dashed = toBeGuessed.replaceAll(".", "-"); // ------
现在,用户作为猜测的字母输入i
。我们将其转换为小写以进行以后的比较。
final char letter = Character.toLowerCase('i');
我们需要做的是更新虚线的String
,为此我们将使用StringBuilder
。
使用StringBuilder
允许我们设置单个字符。
// Create the StringBuilder starting from ------
final StringBuilder sb = new StringBuilder(dashes);
// Loop the String "Italia"
for (int i = 0; i < toBeGuessed.length(); i++) {
final char toBeGuessedChar = toBeGuessed.charAt(i);
// Is the character at the index "i" what we are looking for?
// Remember to transform the character to the same form as the
// guessed letter, maybe lowercase
final char c = Character.toLowerCase(toBeGuessedChar);
if (c == letter) {
// Yes! Update the StringBuilder
sb.setCharAt(i, toBeGuessedChar);
}
}
// Get the final result
final String result = sb.toString();
result
将是I---i-
。
,如java字符串是不可变的,因此您不能用另一个字符串替换任何字符串。
hintsQuestionString = hintsQuestionString.replace(hintsQuestionString.charAt(k), letterChar);
上线在Java中不起作用。
您可以用户StringBuilder类替换字符串
或
您不必吐出字符串,因为它在Java中不起作用,因为Java中的字符串类是不变的。
您可以简单地在textview中设置文本
您从用户那里得到的字符串 字符串hintsquestionstring = hintsquestion.getText()。tostring();
然后使用等价方法比较整个字符串,如果输入的字符串将匹配,则必须设置文本。我已经独自占据了乡下人的变量,您必须用您的带动变量替换此字符串。
if(countryName.equals(hintsQuestionString))
{
hintsQuestion.setText(hintsQuestionString);
}
我希望,这对您有帮助。