Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关于小于和负值?_Java - Fatal编程技术网

Java 关于小于和负值?

Java 关于小于和负值?,java,Java,我有一个初学者的小问题,是关于下面的代码。我想检查客户从信用账户扣除的金额+当前余额是否不低于-5000?它不起作用了,我想知道我做错了什么?是关于负值吗?救命啊 // deduct credit account if(type == "credit") { if((amountOut + creditAccountList.get(index).getAccountBalance()) < -5000) { System.out.println("Sorry! No

我有一个初学者的小问题,是关于下面的代码。我想检查客户从信用账户扣除的金额+当前余额是否不低于-5000?它不起作用了,我想知道我做错了什么?是关于负值吗?救命啊

// deduct credit account
if(type == "credit") {
    if((amountOut + creditAccountList.get(index).getAccountBalance()) < -5000) {
       System.out.println("Sorry! No deduct");
    }
}
//扣除信用账户
如果(类型=“信用”){
如果((amountOut+creditAccountList.get(index.getAccountBalance())<-5000){
System.out.println(“对不起!不扣除”);
}
}
编辑:
amountOut是客户输入的正数。我还是不能让它工作!?可以用其他方式吗?现在对我来说,它看起来很简单,但却很复杂!我只是想防止客户在余额为-5000时扣除某个值?问题是余额是一个负数。

我认为你需要减去一个数值,尽管它不清楚你在其中存储了什么值

if((creditAccountList.get(index).getAccountBalance() - amountOut) < -5000)
                                                     ^
                                              Changed from plus
if((creditAccountList.get(index.getAccountBalance()-amountOut)<-5000)
^
从加号改为加号
更换

if(type == "credit") {

除此之外,代码似乎是正确的,为了进行双重检查,我将通过调试器运行它,或者放置以下错误消息,以便快速轻松地进行调试:

System.out.printf("out %d balance %d\n", amountOut, creditAccountList.get(index).getAccountBalance());
应该是

// deduct credit account
if(type == "credit") {
    if((amountOut + creditAccountList.get(index).getAccountBalance()) > -5000){
   System.out.println("Sorry! No deduct");
}
}

负数的工作原理与往常一样;更有可能是你的逻辑出了问题

if((amountOut + creditAccountList.get(index).getAccountBalance()) < -5000){
if((amountOut+creditAccountList.get(index.getAccountBalance())<-5000){

如果
amountOut
为正数,您将永远不会减少帐户余额。要么将其设为负数,要么从当前余额中减去它,以获得提款后的余额。

我认为您必须重写条件。如果我理解您的情况,客户可以提取比存款金额多5000英镑的金额。例如,如果托默有2000卢比的余额,他/她最多可以支取7000卢比。如果他/她想在上午支取,那么你的申请将显示“对不起!没有扣除”消息。如果是这种情况,那么你可以按照下面给出的逻辑重新编写

int thresholdMoney = 5000;
        if(((creditAccountList.get(index).getAccountBalance()+ thresholdMoney) - Math.abs(amountOut)) < 0){
            System.out.println("Sorry! No deduct");

        }
int thresholdMoney=5000;
if(((creditAccountList.get(index.getAccountBalance()+thresholdMoney)-Math.abs(amountOut))<0){
System.out.println(“对不起!不扣除”);
}

什么是“不工作”的样子?它到底是如何“不工作”的?是否存在编译器错误?运行时错误?错误的结果?getAccountBalance()是什么类型的返回?如果没有更多信息,就无法帮助您。amountOut+ve还是-ve?您的代码需要是-ve才能正常工作。抱歉,我错过了这些信息!嗯,看起来好像没有删除它,也没有打印消息。但我现在正在检查我的代码。也许我错过了什么。情况是,客户的信用额度为5000,让我们来看看比如,卢比,我的想法是允许客户扣除/删除不同的值,直到账户余额达到-5000卢比。如果客户存款/插入货币,使余额为正,则客户不使用信用。您尝试过我发布的逻辑吗?是的,现在我尝试过了,效果很好!谢谢!代码刚刚好有点困惑,但我想我现在明白了。
int thresholdMoney = 5000;
        if(((creditAccountList.get(index).getAccountBalance()+ thresholdMoney) - Math.abs(amountOut)) < 0){
            System.out.println("Sorry! No deduct");

        }