如果语句只能正确执行一次

huangapple go评论84阅读模式
英文:

If statement just works correctly once

问题

这是您的翻译内容:

我有一个简单的应用程序它生成一个随机字符串并将其显示为TextView它会用它们的音译替换字母如果用户输入了正确的音译它会打印正确”,否则它会打印不完全正确实际上是:” + 正确的音译但是它只在第一次运行时才能正常工作之后即使用户输入了正确的音译它也会显示:“不完全正确实际上是:”。

通过打印答案我检查了它是否正确检测用户输入它确实可以但仍然无法正常工作我不确定我的代码的哪个部分出了问题

我将不胜感激任何帮助

这是我的MainActivity的简单形式

```java
package com.example.test;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    String wordStr;
    TextView resultTextView;
    EditText answerText;
    String result;
    TextView randomText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resultTextView = (TextView) findViewById(R.id.result);
        answerText = (EditText) findViewById(R.id.answer);
        randomText = (TextView) findViewById(R.id.randomText);

        findViewById(R.id.newBtn).setOnClickListener(buttonClickListener);
        findViewById(R.id.check).setOnClickListener(buttonClickListener);
        generateRandomStr();
    }

    //生成随机单词

    public void generateRandomStr(){

        String Chars = "Бд";
        StringBuilder word = new StringBuilder();
        Random rnd = new Random();
        while (word.length() < 3) {
            int index = (int) (rnd.nextFloat() * Chars.length());
            word.append(Chars.charAt(index));
        }

        wordStr = word.toString();
        randomText.setText(wordStr);
    }

    private  View.OnClickListener buttonClickListener = new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(View view) {

            switch (view.getId()) {
                //检查按钮
                case R.id.check:

                    String answer = answerText.getText().toString();
                    String transliteration = wordStr.replace("Б", "b").replace("д", "d");
                    if (answer.equals(transliteration)) {
                        result = "正确";
                    } else {
                        result = "不完全正确,实际上是:" + " " + transliteration;
                    }
                    resultTextView.setText(result + " ");

                    break;
                //新按钮
                case R.id.newBtn:

                    resultTextView.setText(" ");
                    answerText.setText(" ");
                    generateRandomStr();
                    break;

            }
        }
    };
}

这是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/newBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="新"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.682"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.528" />

    <Button
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="检查"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.314"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.528" />

    <TextView
        android:id="@+id/randomText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.136" />

    <EditText
        android:id="@+id/answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.371" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.726" />
</androidx.constraintlayout.widget.ConstraintLayout>

提前感谢您的帮助。

英文:

I have a simple app which generates a random String and display it as a TextView. It replaces the letters with their transliterations. If the user types the correct transliteration, it prints "That's correct" otherwise it prints "Not exactly, it's: " + the correct transliteration. However, it works correctly just for the first time. After that, even when the user types the correct transliteration it says: "Not exactly, it's:".
By printing the answer, I checked if it detects the user input correctly; It does but it still doesn't work. I'm not sure which part of my code makes trouble.
I'd appreciate any help.

Here is a simple form of my MainActivity:

package com.example.test;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
//Letter activity
public class MainActivity extends AppCompatActivity {
String wordStr;
TextView resultTextView;
EditText answerText;
String result;
TextView randomText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = (TextView) findViewById(R.id.result);
answerText = (EditText) findViewById(R.id.answer);
randomText = (TextView) findViewById(R.id.randomText);
findViewById(R.id.newBtn).setOnClickListener(buttonClickListener);
findViewById(R.id.check).setOnClickListener(buttonClickListener);
generateRandomStr();
}
//generates random word
public void generateRandomStr(){
String Chars = &quot;Бд&quot;;
StringBuilder word = new StringBuilder();
Random rnd = new Random();
while (word.length() &lt; 3) {
int index = (int) (rnd.nextFloat() * Chars.length());
word.append(Chars.charAt(index));
}
wordStr = word.toString();
randomText.setText(wordStr);
}
private  View.OnClickListener buttonClickListener = new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
switch (view.getId()) {
//Check button
case R.id.check:
String answer = answerText.getText().toString();
String transliteration = wordStr.replace(&quot;Б&quot;, &quot;b&quot;).replace(&quot;д&quot;, &quot;d&quot;);
if (answer.equals(transliteration)) {
result = &quot;That&#39;s correct&quot;;
} else {
result = &quot;Not exactly, it&#39;s actually:&quot; + &quot; &quot; + transliteration;
}
resultTextView.setText(result + &quot; &quot;);
break;
//New button
case R.id.newBtn:
resultTextView.setText(&quot; &quot;);
answerText.setText(&quot; &quot;);
generateRandomStr();
break;
}
}
};
}

And here is activity_main.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;Button
android:id=&quot;@+id/newBtn&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;New&quot;
android:textSize=&quot;18sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.682&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.528&quot; /&gt;
&lt;Button
android:id=&quot;@+id/check&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Check&quot;
android:textSize=&quot;18sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.314&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.528&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/randomText&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot;
android:textSize=&quot;36sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.498&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.136&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/answer&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:inputType=&quot;textPersonName&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.497&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.371&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/result&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Result&quot;
android:textSize=&quot;24sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.726&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Thanks in advance

答案1

得分: 0

在“点击”下一按钮时,您在前面多加了一个额外的空格。可以使用trim()方法去除空格。

String answer = answerText.getText().toString().trim();

或者使用setText("")代替setText(" ")

英文:

You are adding an extra space in front when next button is clicked. Either remove the space by using trim() method.

String answer = answerText.getText().toString().trim();

or use setText(&quot;&quot;) instead of setText(&quot; &quot;).

huangapple
  • 本文由 发表于 2020年9月2日 15:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63701127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定