编程新手遇到问题

时间:2020-04-08 21:52:12

标签: java syntax

我知道\ n应该在使用时为我提供新的一行。在文本前加上\ n,它将在文本之前放置一个新行,在文本之后放置一个新行。但是,我已经编写了代码,请参见下文,除非我也将\ n也放入所创建类的方法中,否则不会创建新行。有人可以帮我理解为什么吗?

在我的方法的system.out.println行中没有\ n,这不会发生。它会像这样出来:

Houston Store:
Gross Revenue is $...
Seattle Store:
Gross Revenue is $

我希望它看起来像这样:

Houston Store:
Gross Revenue is $...

Seattle Store:
Gross Revenue is $....

代码:

        System.out.println("Houston Store:");
        houstonStore.grossRevenue();

        System.out.println("\nSeattle Store: ");
        seattleStore.grossRevenue();

        System.out.println("\nOrlando Store: ");
        orlandoStore.grossRevenue();
    }

}
class groceryStore {

    int applesSoldYearly;
    double priceOfApples;
    int orangesSoldYearly;
    double priceOfOranges; 

    //methods to calculate gross revenue & then print to the screen when called

    void grossRevenue() {
        double revenue;

        revenue = (applesSoldYearly * priceOfApples) 
                + (orangesSoldYearly * priceOfOranges);

        System.out.print ("Gross Revenue is $" + revenue);

    }

1 个答案:

答案 0 :(得分:0)

我认为您的困惑来自System.out.println和System.out.print(不带'ln')的混合使用。由于您在GrossRevenue中使用System.out.print,因此不会在末尾添加换行符。将该语句更改为使用System.out.println应该会为您提供预期的输出。