Java/Android:第一款小应用总是回答“永远”;假;
我试图创建我的第一个小应用程序,但我有一个问题 我的小数学应用程序总是说我的答案是假的。我不明白为什么 这是我的第一个应用,我不知道我错在哪里Java/Android:第一款小应用总是回答“永远”;假;,java,android,Java,Android,我试图创建我的第一个小应用程序,但我有一个问题 我的小数学应用程序总是说我的答案是假的。我不明白为什么 这是我的第一个应用,我不知道我错在哪里 private TextView problem; private EditText question; private Button button; private TextView reponse; private int aleatoire = new Random().nextInt(61) + 20; private int aleatoire
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
回答始终为“假”
我是开发领域的新学生。正如azurefrog评论所说,您正在将字符串与int进行比较。您需要将
str
转换为int
或结果
转换为字符串
。例如,您可以执行以下操作:
String str=question.getText().toString();
if (str.equals(String.valueOf(result))) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
正如azurefrog评论所说,您正在将字符串与int进行比较。您需要将
str
转换为int
或结果
转换为字符串
。例如,您可以执行以下操作:
String str=question.getText().toString();
if (str.equals(String.valueOf(result))) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
代码的问题在于您正在比较字符串和整数,这就是为什么它总是返回false,因为java是严格类型化的语言 问题代码:
if(str.equals(result)){...}
可能的解决办法:
if( str.equals(""+result)){...}
或
或
下面是更正的代码:
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(""+result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
代码的问题在于您正在比较字符串和整数,这就是为什么它总是返回false,因为java是严格类型化的语言 问题代码:
if(str.equals(result)){...}
可能的解决办法:
if( str.equals(""+result)){...}
或
或
下面是更正的代码:
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(""+result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
字符串
永远不会是equals()
到int
。您需要将其中一个转换为与另一个相同的类型,然后进行比较。您需要将字符串转换为整数,您可以使用int strx=Integer.parseInt(str)在比较itA之前,String
永远不会是equals()
到int
。您需要将其中一个转换为与另一个相同的类型,然后进行比较。您需要将字符串转换为整数,您可以使用int strx=Integer.parseInt(str)代码>在比较之前