仅' else'如果其他'我的Android应用中的声明
每当我运行该应用程序并输入4作为答案时,它仍会显示不正确(来自其他语句)。 (我对Android编码是相对较新的,并且找不到在线的精确解决方案。)
这是我的代码:
public class MainActivity extends AppCompatActivity {
EditText answer;
ImageButton ttsBtn;
Button submitBtn;
TextToSpeech textToSpeech;
TextView txt;
int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answer = (EditText)findViewById(R.id.answer);
ttsBtn = (ImageButton) findViewById(R.id.ttsButton);
submitBtn = (Button)findViewById(R.id.submitBtn);
txt = (TextView) findViewById(R.id.question);
String ans = answer.getText().toString();
if (!ans.isEmpty())
{num = Integer.parseInt(answer.getText().toString());}
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if(i!=TextToSpeech.ERROR){
// To Choose language of speech
textToSpeech.setLanguage(Locale.UK);
}
}
});
ttsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newSpeech = "What is 2 multiplied by 2?";
textToSpeech.speak(newSpeech,TextToSpeech.QUEUE_FLUSH,null);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (num == 4){
Toast.makeText(MainActivity.this, "Answer is Correct!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Answer is Incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Whenever I run the app, and type in 4 as the answer, it's still showing incorrect (from the else statement). ( I am relatively new to Android coding, and couldn't find an exact solution online.)
Here is my code:
public class MainActivity extends AppCompatActivity {
EditText answer;
ImageButton ttsBtn;
Button submitBtn;
TextToSpeech textToSpeech;
TextView txt;
int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answer = (EditText)findViewById(R.id.answer);
ttsBtn = (ImageButton) findViewById(R.id.ttsButton);
submitBtn = (Button)findViewById(R.id.submitBtn);
txt = (TextView) findViewById(R.id.question);
String ans = answer.getText().toString();
if (!ans.isEmpty())
{num = Integer.parseInt(answer.getText().toString());}
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if(i!=TextToSpeech.ERROR){
// To Choose language of speech
textToSpeech.setLanguage(Locale.UK);
}
}
});
ttsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newSpeech = "What is 2 multiplied by 2?";
textToSpeech.speak(newSpeech,TextToSpeech.QUEUE_FLUSH,null);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (num == 4){
Toast.makeText(MainActivity.this, "Answer is Correct!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Answer is Incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用以下代码替换您的cumbtbtn onclick。问题是您没有分配最新的答案价值。
Replace your submitBtn onclick with below code. Issue is that you are not assigning latest value of answer.