繁体   English   中英

Java If / Else语句决策

[英]Java If/Else Statement Decision

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

脚本

我的客户欠债$ n

我的客户想还款,我允许他/她以点数付款

所以这是问题所在

最低付款额为500美元 ,这意味着您不能支付低于500美元的费用,应支付的最低付款额应为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();
            }

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

试试吧
为了这

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();
}

假如说 :

  • inputVal是用户输入
  • 主要是要支付的总金额

计算输入是否有效。
如果输入等于要支付的总计,则验证。
否则:如果输入大于或等于最小值(500)且剩余(作为主输入)大于或等于500并且输入小于最大值(主),则输入有效。

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM