英文:
IF statement not work as intended. Cant get the AND to work
问题
我有两个“EditText”,需要它们都为true才能访问第二页,但是即使在单击按钮时两者都为false,它也会访问第二页。
public void DoSubmit(View view) {
String guessSize = weight.getText().toString();
String guessSize2 = height.getText().toString();
if ((Integer.parseInt(guessSize) <= 20 || Integer.parseInt(guessSize) >= 200) && (Integer.parseInt(guessSize2) <= 80 || Integer.parseInt(guessSize2) >= 300)) {
Toast.makeText(MainActivity.this, "请输入20kg到200kg之间的值", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "请输入20kg到200kg之间的值", Toast.LENGTH_SHORT).show();
}
else {
Intent summaryActivity = new Intent(view.getContext(), secondpage.class);
int weightkg = Integer.valueOf(weight.getText().toString());
int heightcm = Integer.valueOf(height.getText().toString());
summaryActivity.putExtra("weight", weightkg);
summaryActivity.putExtra("height", heightcm);
startActivity(summaryActivity); // 启动新页面
}
}
英文:
I have two Editext
and need them both to be true to access the second page, but even if both are false when you click on the button it accesses the second page regardless.
public void DoSubmit(View view) {
String guessSize = weight.getText().toString();
String guessSize2 = height.getText().toString();
if ((Integer.parseInt(guessSize) <= 20 || Integer.parseInt(guessSize) >= 200) & (Integer.parseInt(guessSize2) <= 80 || Integer.parseInt(guessSize2) >= 300) ) {
Toast.makeText(MainActivity.this, "Enter value between 20kg and 200kg", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "Enter value between 20kg and 200kg", Toast.LENGTH_SHORT).show();
}
else {
Intent summaryActivity = new Intent(view.getContext(), secondpage.class);
int weightkg = Integer.valueOf(weight.getText().toString());
int heightcm = Integer.valueOf(height.getText().toString());
summaryActivity.putExtra("weight", weightkg);
summaryActivity.putExtra("height", heightcm);
startActivity(summaryActivity); // start the new page
}
}
答案1
得分: 2
使用 &&
而不是 &
。&&
是逻辑与操作,而 &
是按位与操作。
英文:
Use && not &. && is a logical AND, & is bitwise.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论