在for循环中只需要打印一次字符串-简单的初学者银行程序

时间:2019-03-07 22:30:32

标签: java

编程新手。我必须编写一个简单的银行程序。如果我超出或保持在余额范围内,则可以正常工作。如果我确切地提取剩余的金额,它仍然可以工作并打印无法提取的剩余金额,但是“无法协助以下贷款请求”行。将不会被打印。

如果我在循环中的if语句的最后一行放置该行,它将重复多次以剩余贷款的次数,因此我只需要出现一次即可。弯下腰,已经花了几个小时了,无法解决。总的来说,它可以工作95%。最后必须如下图所示。感谢任何帮助

image of end

public class Exercise2 {

public static void main (String[] args) {

    int loanRequests[] = {80,20,100,300,50,65,100,15};
    int balance = 500;
    String pot = "Cash in the pot: ";
    String request = "Loan amount requested: ";


    for (int i = 0; i < loanRequests.length; i++) {
        if (balance >= loanRequests[i])  {
            System.out.println(pot + balance);               
            System.out.println(request + loanRequests[i] + " - Loan amount granted! " +  "\n");
            balance = (balance - loanRequests[i]);
       }else if (balance < loanRequests[i] && balance > 0) {
           System.out.println(pot + balance + "\n" + request + loanRequests[i]);
           System.out.println("The exact loan amount cannot be processed in full (insufficent funds available).");
           System.out.println("However we will give you what we can... " + balance);
           System.out.println("\n"+ pot + (balance - balance) + "\n" +  "\n" + "The following amounts could not be facilitated");
           balance = 0;
       }else 
          System.out.println(loanRequests[i]);

        }
    }

}

1 个答案:

答案 0 :(得分:0)

嗨,我不确定我是否理解你的意思。但我将提供我认为对您的问题的答案。这将打印出“无法协助以下贷款请求”。只有一次。

public class Exercise2 {

  static String getRestLoanRequestsString(int index,int requests[]){
    StringBuilder builder = new StringBuilder(); //good practice to use StringBuilder when concating strings in loop
    builder.append("The following loan requests could not be facilitated:\n");
    for(; index < requests.length;index++){
        builder.append(requests[index])
                .append("\n");
    }
    return builder.toString();
  }

public static void main (String[] args) {

    int loanRequests[] = {80,20,100,300,50,65,100,15};
    int balance = 500;
    String pot = "Cash in the pot: ";
    String request = "Loan amount requested: ";


    for (int i = 0; i < loanRequests.length; i++) {
        if (balance >= loanRequests[i])  {
            System.out.println(pot + balance);               
            System.out.println(request + loanRequests[i] + " - Loan amount granted! " +  "\n");
            balance = (balance - loanRequests[i]);
       }else if (balance < loanRequests[i] && balance > 0) {
           System.out.println(pot + balance + "\n" + request + loanRequests[i]);
           System.out.println("The exact loan amount cannot be processed in full (insufficent funds available).");
           System.out.println("However we will give you what we can... " + balance);
           System.out.println("\n"+ pot + (balance - balance) + "\n" +  "\n" + "The following amounts could not be facilitated");
           balance = 0;
        }else {
          System.out.println(getRestLoanRequestsString(i,loanRequests));
          break; //break the for loop because we don't have funds to give more loans
        }
    }
}
}