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

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

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

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

jeck貓 2022-07-27 21:39:13
我目前正在使用 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);    }我知道問題在于使用 k 索引,但不確定如何解決這個問題,因為它在 for 循環中。
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

假設您有一個開始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.

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


// 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();


查看完整回答
反對 回復 2022-07-27
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

由于在 Java 中字符串是不可變的,因此您不能用另一個字符串替換任何字符串。

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

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

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

或者

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

您可以簡單地在 TextView 中設置文本

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

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

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

我希望這能幫到您。



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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