简体   繁体   中英

New to programming having problems

I know that the \n should provide me with a new line when utilized. With \n before text being it will put a new line before the text and with it after will create a new line after the text. However, I have written my code, see below, and the new line isn't created unless I also put the \n in the method of my created class as well. Would someone please help me understand why?

Without the \n in my system.out.println line of my method, this doesn't happen. It will come out like this:

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

I want it to look like this:

Houston Store:
Gross Revenue is $...

Seattle Store:
Gross Revenue is $....

code:

        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);

    }

I think your confusion is coming from your mixed use of System.out.println and System.out.print (without the 'ln'). Since you are using System.out.print in grossRevenue, it isn't adding the newline at the end. Changing that statement to use System.out.println should give you the intended output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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