【问题标题】:Java code issues using eclipse IDE (beginner programmaer)使用 Eclipse IDE 的 Java 代码问题(初级程序员)
【发布时间】:2020-10-14 06:00:45
【问题描述】:

我正在上我的大学 (Java) 的初学者编程课程,我们的任务是使用 eclipse IDE 将下图中的内容准确地输出到控制台。我想知道如何在购买价格旁边包含“$”字符?如果不把事情搞砸,我无法弄清楚。另外,我想对我的代码以及我可以做些什么来使它更好的任何反馈。谢谢。这是我迄今为止为这项任务所做的:

Screenshot of assignment

到目前为止我的代码:

public class PurchaseReport
{ 
    public static void main(String[] args) 
    {
        
        double shirt=21.99;
        double pants=49.97;
        double shoes=89.50;
        double belt=19.99;
        double coat=129.95;
        double tot=311.40;
   
        
        System.out.println("");
        System.out.println("----------------------------------");
        System.out.println("----------------------------------");
        System.out.println("");
        System.out.println("Purchase Report");
        System.out.println("");
        System.out.println("----------------------------------");
        System.out.println("----------------------------------");
        System.out.println("");
        System.out.printf("Shirt: %26.2f%n%n", shirt);
        System.out.printf("Pants: %26.2f%n%n", pants);
        System.out.printf("Shoes: %26.2f%n%n", shoes);
        System.out.printf("Belt: %27.2f%n%n", belt);
        System.out.printf("Coat: %28.2f", coat);
        System.out.printf("%n%n");
        System.out.println("----------------------------------");
        System.out.println("----------------------------------");
        System.out.println("");
        System.out.printf("Total: %27.2f", tot);

        
    }
}
    

这是我的代码输出到控制台的内容: console output screenshot

【问题讨论】:

  • 仍然没有投票或接受有效的答案

标签: java eclipse debugging console printf


【解决方案1】:

创建一个 NumberFormat 实例,并打印您的变量,如下面的 printf 所示:

NumberFormat nf=NumberFormat.getCurrencyInstance();
System.out.printf("Shirt %36s", nf.format(shirt));

注意,你可以重复 printf 语句,但整个类只需要一个 NumberFormat 实例,希望对你有帮助

【讨论】:

    【解决方案2】:

    如果您想在购买价格旁边看到 $ 符号,则可以尝试此解决方案

        public class PurchaseReport
    { 
        public static void main(String[] args) 
        {
            
            double shirt=21.99;
            double pants=49.97;
            double shoes=89.50;
            double belt=19.99;
            double coat=129.95;
            double tot=311.40;
       
            
            System.out.println("");
            System.out.println("----------------------------------");
            System.out.println("----------------------------------");
            System.out.println("");
            System.out.println("Purchase Report");
            System.out.println("");
            System.out.println("----------------------------------");
            System.out.println("----------------------------------");
            System.out.println("");
            System.out.printf("Shirt: %26s%n%n",String.format("$%1.2f",shirt) );
            System.out.printf("Pants: %26s%n%n", String.format("$%1.2f",pants));
            System.out.printf("Shoes: %26s%n%n", String.format("$%1.2f",shoes));
            System.out.printf("Belt: %27s%n%n",String.format("$%1.2f",belt) );
            System.out.printf("Coat: %28s",String.format("$%1.2f",coat) );
            System.out.printf("%n%n");
            System.out.println("----------------------------------");
            System.out.println("----------------------------------");
            System.out.println("");
            System.out.printf("Total: %27s", String.format("$%1.2f",tot));
    
            
        }
    }
    

    输出看起来像:

    【讨论】:

    • 这不是美国货币的格式,美元符号在数字之前
    • 你想看开头吗?
    • 是的,数字前的美元符号。谢谢!
    • 答案已更新,所以我认为它会达到您的目的
    【解决方案3】:

    您可以在打印语句中添加$符号 喜欢 System.out.println("衬衫:%$26.2f%n%n", shirt); 这样,您将在输出中获得 $。

    【讨论】:

    • 请确保您完全验证您的答案。正如上述评论所述,您的答案无法编译,因此不正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2012-05-04
    • 2011-05-04
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多