Java 在for循环中为一个字符串使用stringreplace方法

Java 在for循环中为一个字符串使用stringreplace方法,java,android,for-loop,Java,Android,For Loop,我目前正在使用Android Studio 3.2创建一个包含猜旗游戏的移动应用程序。对于其中一个游戏,我必须显示一个随机标志和相应的名称(用破折号覆盖)。 用户可以在下面的编辑文本框中输入一封信,然后单击提交按钮。如果用户得到正确答案,则删除带有该字母的破折号以显示实际字母 对我来说,问题开始于分别更换每个仪表板。当我输入一封信并提交时,所有的破折号都会变成同一封信 package com.example.anisa.assignment1; import android.content.

我目前正在使用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();
    }
}
package com.example.anisa.assignment1;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.TextView;
导入java.util.Random;
公共类猜测提示扩展了AppCompatActivity
{
私有图像视图标志;
私人指数;
公共字符[]答案={};
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u guess\u提示);
displayHintsFlag();
拆分CountryNameLetters();
}
public void displayHintsFlag()
{
flag=findviewbyd(R.id.displayHintsFlag);
随机r=新随机();
int min=0;
int max=255;
随机指数=r.nextInt(最大-最小)+min;
国家=新国家();
int randomHintsFlagImage=countries.countryImages[randIndex];
flag.setImageResource(randomHintsFlagImage);
}
public void splitCountryNameLetters()
{
国家=新国家();
字符串randomHintsFlagName=countries.CountryName[randIndex];
TextView hintsQuestion=(TextView)findViewById(R.id.CountryDashsDisplay);
字符串hintsQuestionString;
int flagname length=randomHintsFlagName.length();
字符字母;
对于(int i=0;i

我知道问题在于k索引的使用,但不确定如何解决这个问题,因为它处于for循环中

在Java中,字符串是不可变的,因此不能用另一个字符串替换任何字符串

 hintsQuestionString = hintsQuestionString.replace(hintsQuestionString.charAt(k), letterChar);
上面这一行在Java中不起作用

您可以使用StringBuilder类替换字符串 或 您不必吐出字符串,因为它在Java中不起作用,因为Java中的字符串类是不可变的。 您只需在TextView中设置文本即可

此字符串是您从用户处获得的 字符串hintsQuestionString=hintsQuestion.getText().toString()

然后使用equals方法比较整个字符串,如果输入的字符串匹配,则必须设置文本。 我自己取了countryName变量,你必须用你取的变量替换这个字符串

if(countryName.equals(hintsQuestionString))
  {
    hintsQuestion.setText(hintsQuestionString);
  }

我希望,这将对您有所帮助。

假设您有一个起始
字符串
,例如
意大利语

用户输入字母
i
,应该发生的是

------ > I---i-
让我们首先将要猜测的
字符串
转换为虚线版本

现在,用户以猜测字母的形式输入
i
。我们将其转换为小写,以便以后比较

final char letter = Character.toLowerCase('i');
我们需要做的是更新虚线
字符串
,为此,我们将使用
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();
//从开始创建StringBuilder------
最终StringBuilder sb=新StringBuilder(破折号);
//循环字符串“Italia”
for(int i=0;i

结果将是
I--I-

我们的答案有用吗?是的
// 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();