java - 如何获得信用额度计划的所有输入总和?

标签 java

我正在完成的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/

相关文章:

java - 如何模拟使用 SLF4J Log4J Logger 的类?

java - 基于属性的 Spring bean

java - 如何测试 Spring Boot 处理程序拦截器

java - 用于投票的 Reddit API

java - Apache DateUtils 截断一周

java - HashSet 和 TreeSet 性能测试

javascript - 检查上传文件是否受 Javascript 或 jQuery 密码保护

java - java中的线程

java - 将 Spring MVC 安全定向更改为登录

java - 将 cookie 注入(inject) WebView 不起作用