简体   繁体   English

基本 Java 练习 - 需要帮助

[英]Basic Java exercise - Help needed

Here are the instructions这是说明

Online Book Depot offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. Online Book Depot 为高级客户每购买 5 本书或更多书提供 1 本书,每购买 8 本书或更多书提供 2 本书免费。

It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books.常客每购买7本书或更多书免费赠送1本书,每购买12本书或更多书免费赠送2本书。

Write the block of code that assigns freeBooks the appropriate value based on the values of the boolean variable isPremiumCustomer and the int variable nbooksPurchased编写代码块,根据布尔变量 isPremiumCustomer 和 int 变量 nbooksPurchased 的值为 freeBooks 分配适当的值

The code compiles without error but results are incorrect:代码编译没有错误,但结果不正确:

boolean isPremiumCustomer = false;
    int freeBooks = 0;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the no of books:");
    int nbooksPurchased = scanner.nextInt();
    System.out.println("is Premium customer?(y/n):");
    String ans = scanner.next();
    if (ans.equals("y") || ans.equals("Y") && (nbooksPurchased >4 && (nbooksPurchased <8))) {
        isPremiumCustomer = true;
        freeBooks = 1;
    }else if (ans.equals("y") || ans.equals("Y") && (nbooksPurchased >=8 )) {
        isPremiumCustomer = true;
        freeBooks = 2;
    }else if (ans.equals("n") || ans.equals("N") && (nbooksPurchased >=7 && (nbooksPurchased <=11))) {
        freeBooks = 1;
    }else if (ans.equals("n") || ans.equals("N") && (nbooksPurchased >=12 )){
        freeBooks = 2;
        }
        System.out.println("Free Books:" + freeBooks);

Reading your own code in the comment is hardly possible.在评论中阅读您自己的代码几乎是不可能的。 When I format it, I get:当我格式化它时,我得到:

if (ans.equals("y") || ans.equals("Y")) {
    isPremiumCustomer = true;
}
if (nbooksPurchased >= 3 && (nbooksPurchased <= 7)) {
    freeBooks = 1;
} else if (nbooksPurchased >= 8) {
    freeBooks = 2;
}
if (ans.equals("n") || ans.equals("N")) {
    if ((nbooksPurchased >= 7 && (nbooksPurchased <= 11))) {
        freeBooks = 1;
    } else if (nbooksPurchased >= 12) {
        freeBooks = 2;
    }
}

The main problem with this is that the middle if-else construct if (nbooksPurchased >= 3 && (nbooksPurchased <= 7)) { is executed for all customers, not only premium customers.这样做的主要问题是中间 if-else 构造if (nbooksPurchased >= 3 && (nbooksPurchased <= 7)) {对所有客户执行,而不仅仅是高级客户。 So regular customers that buy 3 or 6 books get the 1 free book that only premium customers should have had on such a purchase.因此,购买 3 或 6 本书的普通客户将获得 1 本免费书籍,而只有高级客户在购买此类书籍时才能获得。 If they buy 7 or more, the execution gets into if (ans.equals("n") || ans.equals("N")) { , and you're saved.如果他们购买 7 个或更多,则执行进入if (ans.equals("n") || ans.equals("N")) { ,并且您已获救。

The other problem is just a typo, nbooksPurchased >= 3 should have been 5 for premium customers.另一个问题只是一个错字, nbooksPurchased >= 3对于高级客户来说应该是 5。

Most cases work correctly.大多数情况下工作正常。

This would be clearer (and hence easier for yourself to correct errors in):这会更清楚(因此你自己更容易纠正错误):

if (isPremiumCustomer) {
    // put if-else for premium customers here
} else {
    // put if-else for regular customers here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM