亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在一個字符串的 for 循環中使用字符串替換方法

在一個字符串的 for 循環中使用字符串替換方法

jeck貓 2022-08-03 15:22:30
我目前正在使用Android Studio 3.2創建一個包含標志猜測游戲的移動應用程序。對于其中一個游戲,我必須顯示一個隨機標志和相應的名稱(用破折號覆蓋)。用戶可以在下面的編輯文本框中輸入一個字母,然后單擊提交按鈕。如果用戶得到正確的答案,則帶有該字母的破折號將被刪除以顯示實際的字母。顯示應用 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 + "");我知道問題在于使用k索引,但不知道如何解決這個問題,因為它在for循環中。
查看完整描述

2 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

假設您有一個啟動,例如 .

用戶輸入字母,應該發生的事情是StringItaliai


------ > I---i-

讓我們首先將待猜到的版本轉換為虛線版本String


final String toBeGuessed = "Italia";                     // Italia

final String dashed = toBeGuessed.replaceAll(".", "-");  // ------

現在,用戶以猜測的字母形式輸入。我們將其轉換為小寫字母,以便以后進行比較。i


final char letter = Character.toLowerCase('i');

我們需要做的是更新虛線,為此我們將使用.

使用 a 允許我們設置單個字符。StringStringBuilderStringBuilder


// 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-


查看完整回答
反對 回復 2022-08-03
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

就像在Java中一樣,字符串是不可變的,因此您無法用另一個字符串替換任何字符串。


 hintsQuestionString = hintsQuestionString.replace(hintsQuestionString.charAt(k), letterChar);

上面的行在Java中不起作用。


您可以使用 StringBuilder 類來替換字符串

您不必吐出字符串,因為它在Java中不起作用,因為Java中的String類是不可變的。

您只需在文本視圖中設置文本即可


你從用戶那里得到的這個字符串 String hintsQuestionString = hintsQuestion.getText().toString();


然后使用equals方法比較整個字符串,如果輸入的字符串匹配,則必須設置文本。我已經自己獲取了 countryName 變量,您必須將此字符串替換為您采用的變量。


if(countryName.equals(hintsQuestionString))

  {

    hintsQuestion.setText(hintsQuestionString);

  }

我希望,這會幫助你。


查看完整回答
反對 回復 2022-08-03
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號