有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 Java If/Else语句决策

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

场景

我的客户欠了n美元的债

我的客户想还钱,我允许他/她用比特支付

这就是问题所在

Min Payment是$500,这意味着你不能低于$500支付,而Min Payment应该是$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) 个答案

  1. # 1 楼答案

    假设:

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

    计算输入是否有效
    如果输入等于支付总额,则验证
    否则:如果输入大于或等于最小值(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();
    }
    
  2. # 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();
    }