首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在编写简单的Java程序时遇到问题

在编写简单的Java程序时遇到问题
EN

Stack Overflow用户
提问于 2018-10-22 08:07:50
回答 1查看 534关注 0票数 -2

试图写一个Java程序来满足这一点:杜克衬衫销售Java T恤$24.95每件,但折扣是可能的数量如下:1或2衬衫,没有折扣和总运费是$10.00 3-5衬衫,折扣是10%和总运费$8.00 6-10衬衫,折扣是20%和总运费是5.00美元11件或更多的衬衫,折扣是30%和送货是免费的写一个Java程序,提示用户所需的衬衫数量。然后,程序应该打印衬衫的扩展价格、运费和订单的总成本。在适当的情况下使用货币格式。

下面是我的代码:

代码语言:javascript
复制
import java.lang.Math;
import java.util.Scanner;

public class Excercise2_3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("How many shirts do you need?");
        double shirts = input.nextInt();
        if (shirts <= 2)
            System.out.printf("The extended cost is : $%.2", (shirts * 24.95));
            System.out.printf("The shipping charges are $10.00");
            System.out.printf("The total cost is : $%.2", (shirts * 24.95) + 10);
        if (shirts > 2 && shirts <= 5)
            System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.10));
            System.out.printf("The shipping charges are $8.00");
            System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.10) + 8);
        if (shirts > 5 && shirts <= 10)
            System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.20));
            System.out.printf("The shipping charges are $5.00");
            System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.20) + 5);
        if (shirts > 10)
            System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.00));
            System.out.printf("Shipping is free!");
            System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.30));

    }

}

有人能解释一下为什么它不能正确编译吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-22 08:12:14

if语句两边缺少大括号。另外,您的printf格式字符串应该是%.2f。你错过了f,你错过了换行符。

代码语言:javascript
复制
import java.util.Scanner;

public class TempApp {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("How many shirts do you need?");
        double shirts = input.nextInt();
        if (shirts <= 2) {
            System.out.printf("The extended cost is : $%.2f\n", (shirts * 24.95));
            System.out.printf("The shipping charges are $10.00\n");
            System.out.printf("The total cost is : $%.2f\n", (shirts * 24.95) + 10);
        }
        if (shirts > 2 && shirts <= 5) {
            System.out.printf("The extended cost is : $%.2f\n", ((shirts * 24.95) * .10));
            System.out.printf("The shipping charges are $8.00\n");
            System.out.printf("The total cost is : $%.2f\n", ((shirts * 24.95) * .10) + 8);
        }
        if (shirts > 5 && shirts <= 10) {
            System.out.printf("The extended cost is : $%.2f\n", ((shirts * 24.95) * .20));
            System.out.printf("The shipping charges are $5.00\n");
            System.out.printf("The total cost is : $%.2f\n", ((shirts * 24.95) * .20) + 5);
        }
        if (shirts > 10) {
            System.out.printf("The extended cost is : $%.2f", ((shirts * 24.95) * .00));
            System.out.printf("Shipping is free!");
            System.out.printf("The total cost is : $%.2f", ((shirts * 24.95) * .30));
        }
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52920989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档