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

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

If statement just works correctly once

问题

这是您的翻译内容:

  1. 我有一个简单的应用程序它生成一个随机字符串并将其显示为TextView它会用它们的音译替换字母如果用户输入了正确的音译它会打印正确”,否则它会打印不完全正确实际上是:” + 正确的音译但是它只在第一次运行时才能正常工作之后即使用户输入了正确的音译它也会显示:“不完全正确实际上是:”。
  2. 通过打印答案我检查了它是否正确检测用户输入它确实可以但仍然无法正常工作我不确定我的代码的哪个部分出了问题
  3. 我将不胜感激任何帮助
  4. 这是我的MainActivity的简单形式
  5. ```java
  6. package com.example.test;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import androidx.annotation.RequiresApi;
  13. import androidx.appcompat.app.AppCompatActivity;
  14. import java.util.Random;
  15. public class MainActivity extends AppCompatActivity {
  16. String wordStr;
  17. TextView resultTextView;
  18. EditText answerText;
  19. String result;
  20. TextView randomText;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. resultTextView = (TextView) findViewById(R.id.result);
  26. answerText = (EditText) findViewById(R.id.answer);
  27. randomText = (TextView) findViewById(R.id.randomText);
  28. findViewById(R.id.newBtn).setOnClickListener(buttonClickListener);
  29. findViewById(R.id.check).setOnClickListener(buttonClickListener);
  30. generateRandomStr();
  31. }
  32. //生成随机单词
  33. public void generateRandomStr(){
  34. String Chars = "Бд";
  35. StringBuilder word = new StringBuilder();
  36. Random rnd = new Random();
  37. while (word.length() < 3) {
  38. int index = (int) (rnd.nextFloat() * Chars.length());
  39. word.append(Chars.charAt(index));
  40. }
  41. wordStr = word.toString();
  42. randomText.setText(wordStr);
  43. }
  44. private View.OnClickListener buttonClickListener = new View.OnClickListener() {
  45. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  46. @Override
  47. public void onClick(View view) {
  48. switch (view.getId()) {
  49. //检查按钮
  50. case R.id.check:
  51. String answer = answerText.getText().toString();
  52. String transliteration = wordStr.replace("Б", "b").replace("д", "d");
  53. if (answer.equals(transliteration)) {
  54. result = "正确";
  55. } else {
  56. result = "不完全正确,实际上是:" + " " + transliteration;
  57. }
  58. resultTextView.setText(result + " ");
  59. break;
  60. //新按钮
  61. case R.id.newBtn:
  62. resultTextView.setText(" ");
  63. answerText.setText(" ");
  64. generateRandomStr();
  65. break;
  66. }
  67. }
  68. };
  69. }

这是activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/newBtn"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="新"
  13. android:textSize="18sp"
  14. app:layout_constraintBottom_toBottomOf="parent"
  15. app:layout_constraintEnd_toEndOf="parent"
  16. app:layout_constraintHorizontal_bias="0.682"
  17. app:layout_constraintStart_toStartOf="parent"
  18. app:layout_constraintTop_toTopOf="parent"
  19. app:layout_constraintVertical_bias="0.528" />
  20. <Button
  21. android:id="@+id/check"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="检查"
  25. android:textSize="18sp"
  26. app:layout_constraintBottom_toBottomOf="parent"
  27. app:layout_constraintEnd_toEndOf="parent"
  28. app:layout_constraintHorizontal_bias="0.314"
  29. app:layout_constraintStart_toStartOf="parent"
  30. app:layout_constraintTop_toTopOf="parent"
  31. app:layout_constraintVertical_bias="0.528" />
  32. <TextView
  33. android:id="@+id/randomText"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="TextView"
  37. android:textSize="36sp"
  38. app:layout_constraintBottom_toBottomOf="parent"
  39. app:layout_constraintEnd_toEndOf="parent"
  40. app:layout_constraintHorizontal_bias="0.498"
  41. app:layout_constraintStart_toStartOf="parent"
  42. app:layout_constraintTop_toTopOf="parent"
  43. app:layout_constraintVertical_bias="0.136" />
  44. <EditText
  45. android:id="@+id/answer"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:ems="10"
  49. android:inputType="textPersonName"
  50. app:layout_constraintBottom_toBottomOf="parent"
  51. app:layout_constraintEnd_toEndOf="parent"
  52. app:layout_constraintHorizontal_bias="0.497"
  53. app:layout_constraintStart_toStartOf="parent"
  54. app:layout_constraintTop_toTopOf="parent"
  55. app:layout_constraintVertical_bias="0.371" />
  56. <TextView
  57. android:id="@+id/result"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:text="Result"
  61. android:textSize="24sp"
  62. app:layout_constraintBottom_toBottomOf="parent"
  63. app:layout_constraintEnd_toEndOf="parent"
  64. app:layout_constraintStart_toStartOf="parent"
  65. app:layout_constraintTop_toTopOf="parent"
  66. app:layout_constraintVertical_bias="0.726" />
  67. </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:

  1. package com.example.test;
  2. import android.os.Build;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import android.widget.TextView;
  7. import androidx.annotation.RequiresApi;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. import java.util.Random;
  10. //Letter activity
  11. public class MainActivity extends AppCompatActivity {
  12. String wordStr;
  13. TextView resultTextView;
  14. EditText answerText;
  15. String result;
  16. TextView randomText;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. resultTextView = (TextView) findViewById(R.id.result);
  22. answerText = (EditText) findViewById(R.id.answer);
  23. randomText = (TextView) findViewById(R.id.randomText);
  24. findViewById(R.id.newBtn).setOnClickListener(buttonClickListener);
  25. findViewById(R.id.check).setOnClickListener(buttonClickListener);
  26. generateRandomStr();
  27. }
  28. //generates random word
  29. public void generateRandomStr(){
  30. String Chars = &quot;Бд&quot;;
  31. StringBuilder word = new StringBuilder();
  32. Random rnd = new Random();
  33. while (word.length() &lt; 3) {
  34. int index = (int) (rnd.nextFloat() * Chars.length());
  35. word.append(Chars.charAt(index));
  36. }
  37. wordStr = word.toString();
  38. randomText.setText(wordStr);
  39. }
  40. private View.OnClickListener buttonClickListener = new View.OnClickListener() {
  41. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  42. @Override
  43. public void onClick(View view) {
  44. switch (view.getId()) {
  45. //Check button
  46. case R.id.check:
  47. String answer = answerText.getText().toString();
  48. String transliteration = wordStr.replace(&quot;Б&quot;, &quot;b&quot;).replace(&quot;д&quot;, &quot;d&quot;);
  49. if (answer.equals(transliteration)) {
  50. result = &quot;That&#39;s correct&quot;;
  51. } else {
  52. result = &quot;Not exactly, it&#39;s actually:&quot; + &quot; &quot; + transliteration;
  53. }
  54. resultTextView.setText(result + &quot; &quot;);
  55. break;
  56. //New button
  57. case R.id.newBtn:
  58. resultTextView.setText(&quot; &quot;);
  59. answerText.setText(&quot; &quot;);
  60. generateRandomStr();
  61. break;
  62. }
  63. }
  64. };
  65. }

And here is activity_main.xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.MainActivity&quot;&gt;
  8. &lt;Button
  9. android:id=&quot;@+id/newBtn&quot;
  10. android:layout_width=&quot;wrap_content&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:text=&quot;New&quot;
  13. android:textSize=&quot;18sp&quot;
  14. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  15. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  16. app:layout_constraintHorizontal_bias=&quot;0.682&quot;
  17. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  18. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  19. app:layout_constraintVertical_bias=&quot;0.528&quot; /&gt;
  20. &lt;Button
  21. android:id=&quot;@+id/check&quot;
  22. android:layout_width=&quot;wrap_content&quot;
  23. android:layout_height=&quot;wrap_content&quot;
  24. android:text=&quot;Check&quot;
  25. android:textSize=&quot;18sp&quot;
  26. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  27. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  28. app:layout_constraintHorizontal_bias=&quot;0.314&quot;
  29. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  30. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  31. app:layout_constraintVertical_bias=&quot;0.528&quot; /&gt;
  32. &lt;TextView
  33. android:id=&quot;@+id/randomText&quot;
  34. android:layout_width=&quot;wrap_content&quot;
  35. android:layout_height=&quot;wrap_content&quot;
  36. android:text=&quot;TextView&quot;
  37. android:textSize=&quot;36sp&quot;
  38. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  39. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  40. app:layout_constraintHorizontal_bias=&quot;0.498&quot;
  41. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  42. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  43. app:layout_constraintVertical_bias=&quot;0.136&quot; /&gt;
  44. &lt;EditText
  45. android:id=&quot;@+id/answer&quot;
  46. android:layout_width=&quot;wrap_content&quot;
  47. android:layout_height=&quot;wrap_content&quot;
  48. android:ems=&quot;10&quot;
  49. android:inputType=&quot;textPersonName&quot;
  50. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  51. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  52. app:layout_constraintHorizontal_bias=&quot;0.497&quot;
  53. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  54. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  55. app:layout_constraintVertical_bias=&quot;0.371&quot; /&gt;
  56. &lt;TextView
  57. android:id=&quot;@+id/result&quot;
  58. android:layout_width=&quot;wrap_content&quot;
  59. android:layout_height=&quot;wrap_content&quot;
  60. android:text=&quot;Result&quot;
  61. android:textSize=&quot;24sp&quot;
  62. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  63. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  64. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  65. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  66. app:layout_constraintVertical_bias=&quot;0.726&quot; /&gt;
  67. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Thanks in advance

答案1

得分: 0

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

  1. 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.

  1. 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:

确定