我正在完成的java程序要求我“要求用户输入5件购买元素的价格。您的信用额度是150美元。程序将计算元素的总成本并打印出总成本,然后打印无论您是被批准还是被拒绝”。我包含了迄今为止代码的主要部分。
int totalPrice;
int creditLimit;
Scanner input = new Scanner(System.in);
for(int i=0; i<5; i++){
System.out.println("Enter total price of item: ");
totalPrice = input.nextInt();
System.out.println("Enter credit limit: ");
creditLimit = input.nextInt();
}
int sum = ???
System.out.println("The total cost of all items is: " +sum);
问号表明我感到困惑。我不确定我是否走在正确的轨道上,还是完全偏离了轨道。在获得所购买商品的总费用后,如何显示用户是被批准还是被拒绝?
最佳答案
您应该首先添加所有商品的价格。
public static void main(String[] args) {
int totalPrice = 0;
int creditLimit;
Scanner input = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("Enter total price of item: ");
totalPrice += input.nextInt();
}
System.out.println("Enter credit limit: ");
creditLimit = input.nextInt();
System.out.println("The total cost of all items is: " + totalPrice);
System.out.println("Your credit limit is : " + creditLimit);
System.out.println("Evaluation result: " + (totalPrice > creditLimit ? "declined" : "approved"));
}
关于java - 如何获得信用额度计划的所有输入总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58147514/