英文:
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 < 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
得分: 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 < 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;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论