如何在迭代每个元素时从一个值中减去数组的元素?

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

How do you subtract an arrays elements from a value while iterating through each element?

问题

public class Exercise2 {
    
    public static void main(String[] args) {
        
        int savings = 500;
        int[] loanAmount = {60, 20, 100, 80, 40, 300, 200, 100};
        int balance;
        
        for (int i = 0; i < loanAmount.length; i++) {

            balance = savings - loanAmount[i];
        
            System.out.println("现有资金: " + balance + "\n" + "贷款申请金额: " + loanAmount[i] + " - " + " 贷款已批准!");
            System.out.println("剩余资金: " + balance);
            

            if(loanAmount[i] > balance)
            {
             System.out.println("现有资金: " + balance + "\n" + "贷款申请金额: " + loanAmount[i] + "\n" + "无法完全处理所请求的贷款金额(资金不足)。\n" + "但是,我们会尽力提供帮助... " + balance);
            
            }
            
            if(balance == 0)
            {
            System.out.println("以下贷款申请无法获得批准\n" + loanAmount[i] + "\n" + loanAmount[i]);
                
            }
        }
    }        
}
英文:

I need to create a program that is needs to find out which loans can be granted. I have a balance of 500 to start and an array of loan requests and it's done on a first come, first served basis. The problem I'm running into is it not updating my savings.

Any help would be greatly appreciated!

public class Exercise2 {
    
    public static void main(String[] args) {
        
        int savings = 500;
        int[] loanAmount = {60, 20, 100, 80, 40, 300, 200, 100};
        int balance;
        
        for (int i = 0; i &lt; loanAmount.length; i++) {

            balance = savings - loanAmount[i];
        
            System.out.println(&quot;Cash in the pot: &quot; + balance + &quot;\n&quot; + &quot;Loan amount requested: &quot; + loanAmount[i] + &quot; - &quot; + &quot; Loan amount granted!&quot;);
            System.out.println(&quot;Cash remaining in the pot: &quot; + balance);
            

            if(loanAmount[i] &gt; balance)
            {
             System.out.println(&quot;Cash in the pot: &quot; + balance + &quot;\n&quot; + &quot;Loan amount requested: &quot; + loanAmount[i] + &quot;\n&quot; + &quot;The exact loan request amount cannot be processed in full (insufficent funds available).&quot; + &quot;\n&quot; + &quot;However, we will give you what we can... &quot; + balance);
            
            }
            
            if(balance == 0)
            {
            System.out.println(&quot;The following loan requests could not be facilitated&quot; + &quot;\n&quot; + loanAmount[i] + &quot;\n&quot; + loanAmount[i]);
                
            }
        }
    }        
}

答案1

得分: 0

你是在寻找这个吗

package arraysAndStrings;

public class Exercise2 {

    public static void main(String[] args) {

        int savings = 500;
        int[] loanAmount = { 60, 20, 100, 80, 40, 300, 200, 100 };
        // int balance = savings;

        for (int i = 0; i < loanAmount.length; i++) {
            if ((savings - loanAmount[i]) >= 0) {
                System.out.println("现有资金: " + savings + "\n"
                        + "所请求的贷款金额: " + loanAmount[i] + " - "
                        + " 已批准贷款金额!");
                savings -= loanAmount[i];
                System.out.println("剩余现金: " + savings);
                System.out.println();
            } else if (savings > 0) {
                System.out
                        .println("现有资金: "
                                + savings
                                + "\n"
                                + "所请求的贷款金额: "
                                + loanAmount[i]
                                + "\n"
                                + "无法完全处理准确的贷款请求金额(资金不足)。\n"
                                + "不过,我们会尽力提供给您... "
                                + savings);
                System.out.println();
            } else {
                System.out.println("没有剩余现金!");
                break;
            }
        }
    }

}
英文:

Are you looking for this?

    package arraysAndStrings;
public class Exercise2 {
public static void main(String[] args) {
int savings = 500;
int[] loanAmount = { 60, 20, 100, 80, 40, 300, 200, 100 };
// int balance = savings;
for (int i = 0; i &lt; loanAmount.length; i++) {
if ((savings - loanAmount[i]) &gt;= 0) {
System.out.println(&quot;Cash in the pot: &quot; + savings + &quot;\n&quot;
+ &quot;Loan amount requested: &quot; + loanAmount[i] + &quot; - &quot;
+ &quot; Loan amount granted!&quot;);
savings -= loanAmount[i];
System.out.println(&quot;Cash remaining in the pot: &quot; + savings);
System.out.println();
} else if (savings &gt; 0) {
System.out
.println(&quot;Cash in the pot: &quot;
+ savings
+ &quot;\n&quot;
+ &quot;Loan amount requested: &quot;
+ loanAmount[i]
+ &quot;\n&quot;
+ &quot;The exact loan request amount cannot be processed in full (insufficent funds available).&quot;
+ &quot;\n&quot;
+ &quot;However, we will give you what we can... &quot;
+ savings);
System.out.println();
} else {
System.out.println(&quot;No cash left!&quot;);
break;
}
}
}
}

huangapple
  • 本文由 发表于 2020年9月8日 23:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63796947.html
匿名

发表评论

匿名网友

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

确定