Java If / Else语句决定

时间:2015-09-29 19:45:13

标签: java android

我真的对如何在Android Java中构造这些语句的if/else感到困惑。

方案

我的客户负债为$ n

我的客户想要还款,我允许他/她付款

所以,这是问题

最低付款 $ 500 ,这意味着您无法支付 $ 500 以及应该留下的Min应该是 $ 500 ,这意味着您 $ 900 您无法支付 $ 400 $ 500 您需要支付 900 。所以,这就是我做的事情

if (inputVal < 500 || inputVal > main) {
            if (inputVal < 500) {
                amount_to_pay.setError("Min Charge: 500");
                pay_of_loan.setEnabled(false);
            }
            if (inputVal > main) {
                amount_to_pay.setError("Max Charge: " + ccNum);
                pay_of_loan.setEnabled(false);
            }
        } else {
            amount_to_pay.setError(null);
            pay_of_loan.setEnabled(true);
        }

当按钮通过上面的if/else

时启用
 if (inputVal - main < 500 && inputVal != main) {
                Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
            }

但是,它永远不会验证。任何有关逻辑的帮助将不胜感激。感谢

2 个答案:

答案 0 :(得分:2)

试试这个

if (inputVal < 500 || inputVal > main) {
            if (inputVal < 500) {
                amount_to_pay.setError("Min Charge: 500");
                pay_of_loan.setEnabled(false);
            }
            if (inputVal > main) {
                amount_to_pay.setError("Max Charge: " + ccNum);
                pay_of_loan.setEnabled(false);
            }
        } else {
            amount_to_pay.setError(null);
            pay_of_loan.setEnabled(true);
        }

if (inputVal < 500) {
   amount_to_pay.setError("Min Charge: 500");
   pay_of_loan.setEnabled(false);
}else if (inputVal > main) {
   amount_to_pay.setError("Max Charge: " + ccNum);
   pay_of_loan.setEnabled(false);
}else if ((main - inputVal) <500) {
   amount_to_pay.setError("For partial pays, min remaining: " + 500);
   pay_of_loan.setEnabled(false);
}else {
   amount_to_pay.setError(null);
   pay_of_loan.setEnabled(true);
}

if (inputVal - main < 500 && inputVal != main) {
                Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
            }

if (((main - inputVal) < 500) && (inputVal != main)) {
   Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
} else {
   Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:2)

假设:

  • inputVal是用户输入
  • main是支付的总额

计算输入是否有效 如果输入等于总付款验证 否则:如果输入大于或等于minimun(500)且剩余(主输入)大于或等于500且输入小于maximun(main),则输入有效。

bool inputIsValid( int input, int min, int max )
{
  if ( input==max )
    return true;
  int remain = max - input;
  return input>=min && remain>=min && input<max;
}

您将其用作:

if (!inputIsValid( inputVal, 500, main) {
  if (inputVal < 500) {
    amount_to_pay.setError("Min Charge: 500");
    pay_of_loan.setEnabled(false);
  }
  else if (inputVal > main) {
    amount_to_pay.setError("Max Charge: " + ccNum);
    pay_of_loan.setEnabled(false);
  }
  else
  {
    amount_to_pay.setError("For partial pays, min remaining: " + 500);
    pay_of_loan.setEnabled(false);
  }
} else {
  amount_to_pay.setError(null);
  pay_of_loan.setEnabled(true);
}

并且:

if (!inputIsValid( inputVal, 500, main ) {
  Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
} else {
  Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
}