有 Java 编程相关的问题?

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

java如何在遍历每个元素时从值中减去数组元素?

我需要制定一个计划,找出哪些贷款可以发放。我还有500英镑的余额要开始,还有一系列贷款申请,都是先到先得的。我遇到的问题是没有更新我的储蓄

任何帮助都将不胜感激

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("Cash in the pot: " + balance + "\n" + "Loan amount requested: " + loanAmount[i] + " - " + " Loan amount granted!");
            System.out.println("Cash remaining in the pot: " + balance);
            

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

共 (1) 个答案

  1. # 1 楼答案

    你在找这个吗

        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("Cash in the pot: " + savings + "\n"
                            + "Loan amount requested: " + loanAmount[i] + " - "
                            + " Loan amount granted!");
                    savings -= loanAmount[i];
                    System.out.println("Cash remaining in the pot: " + savings);
                    System.out.println();
                } else if (savings > 0) {
                    System.out
                            .println("Cash in the pot: "
                                    + savings
                                    + "\n"
                                    + "Loan amount requested: "
                                    + loanAmount[i]
                                    + "\n"
                                    + "The exact loan request amount cannot be processed in full (insufficent funds available)."
                                    + "\n"
                                    + "However, we will give you what we can... "
                                    + savings);
                    System.out.println();
                } else {
                    System.out.println("No cash left!");
                    break;
                }
            }
        }
    
    }