IF语句的工作不如预期。无法使AND起作用。

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

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) &lt;= 20 || Integer.parseInt(guessSize) &gt;= 200) &amp; (Integer.parseInt(guessSize2) &lt;= 80 || Integer.parseInt(guessSize2) &gt;= 300) ) {
            Toast.makeText(MainActivity.this, &quot;Enter value between 20kg and 200kg&quot;, Toast.LENGTH_SHORT).show();
            Toast.makeText(MainActivity.this, &quot;Enter value between 20kg and 200kg&quot;, 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(&quot;weight&quot;, weightkg);
            summaryActivity.putExtra(&quot;height&quot;, heightcm);
            startActivity(summaryActivity);     // start the new page
        }
        

    }

答案1

得分: 2

使用 && 而不是 &&& 是逻辑与操作,而 & 是按位与操作。

英文:

Use && not &. && is a logical AND, & is bitwise.

huangapple
  • 本文由 发表于 2020年10月15日 18:21:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64369501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定