英文:
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 = "Бд";
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()) {
//Check button
case R.id.check:
String answer = answerText.getText().toString();
String transliteration = wordStr.replace("Б", "b").replace("д", "d");
if (answer.equals(transliteration)) {
result = "That's correct";
} else {
result = "Not exactly, it's actually:" + " " + transliteration;
}
resultTextView.setText(result + " ");
break;
//New button
case R.id.newBtn:
resultTextView.setText(" ");
answerText.setText(" ");
generateRandomStr();
break;
}
}
};
}
And here is 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="New"
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="Check"
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>
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("")
instead of setText(" ")
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论