我目前正在使用Android Studio 3.2创建一个包含标记猜谜游戏的移动应用程序。对于其中一款游戏,我必须显示一个随机标志和相应的名称(用破折号覆盖)。 用户可以在下面的编辑文本框中输入字母,然后单击提交按钮。如果用户正确回答,则带有该字母的破折号将被删除以显示实际字母。
对我来说,问题开始于分别替换每个破折号。输入字母并提交时,所有破折号都变成相同的字母。
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循环中,因此不确定如何解决此问题。
答案 0 :(得分:1)
假设您有一个起始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-
。
答案 1 :(得分:0)
因为在Java中String是不可变的,所以您不能将任何字符串替换为另一个String。
hintsQuestionString = hintsQuestionString.replace(hintsQuestionString.charAt(k), letterChar);
以上行在Java中不起作用。
您只需在TextView中设置文本
您从用户那里得到的这个字符串 字符串hintsQuestionString = hintsQuestion.getText()。toString();
然后使用equals方法比较整个字符串,如果输入的字符串匹配,则必须设置文本。 我已经单独使用了countryName变量,您必须将此字符串替换为您采用的变量。
if(countryName.equals(hintsQuestionString))
{
hintsQuestion.setText(hintsQuestionString);
}
我希望这会对您有所帮助。