Почему я получаю NaN или ноль в Java? - PullRequest
0 голосов
/ 14 апреля 2020
public class Main {

    public static void main(String[] args) {
        GroceryStore houstonStore = new GroceryStore();
        GroceryStore orlandoStore = new GroceryStore();
        GroceryStore seattleStore = new GroceryStore();

        double houstonStoreRevenue = ( houstonStore.applesSold * houstonStore.applesPrice) + (houstonStore.orangesSold * houstonStore.orangesPrice);
        double orlandoStoreRevenue = (orlandoStore.applesSold * orlandoStore.applesPrice) + (orlandoStore.orangesSold * orlandoStore.orangesPrice );
        double seattleStoreRevenue = (seattleStore.applesSold * seattleStore.applesPrice) + (seattleStore.orangesSold * seattleStore.orangesPrice);


        houstonStore.applesSold = 534;
        houstonStore.applesPrice = 0.99;
        houstonStore.orangesSold = 429;
        houstonStore.orangesPrice = 0.87;
        System.out.println("The Houston store revenue is: " + houstonStoreRevenue);

        seattleStore.applesSold = 765;
        seattleStore.applesPrice = 0.86;
        seattleStore.orangesSold = 842;
        seattleStore.orangesPrice = 0.91;
        System.out.println("The Seattle store revenue is: " + seattleStoreRevenue);

        orlandoStore.applesSold = 402;
        orlandoStore.applesPrice = 0.79;
        orlandoStore.orangesSold = 398;
        orlandoStore.orangesPrice = 0.77;
        System.out.println("The Orlando store revenue is: " + orlandoStoreRevenue);

    }
}
class class GroceryStore {
    int applesSold;
    double applesPrice;
    int orangesSold;
    double orangesPrice;
}

Я не совсем понимаю, почему я получаю ответ ниже

*****
The Houston store revenue is: 0.0
The Seattle store revenue is: 0.0
The Orlando store revenue is: 0.0

1 Ответ

0 голосов
/ 14 апреля 2020

Вы ссылаетесь на атрибуты, которые еще не были объявлены. Попробуйте вместо этого ...

public class Main {

    public static void main(String[] args) {
        GroceryStore houstonStore = new GroceryStore();
        GroceryStore orlandoStore = new GroceryStore();
        GroceryStore seattleStore = new GroceryStore();

        houstonStore.applesSold = 534;
        houstonStore.applesPrice = 0.99;
        houstonStore.orangesSold = 429;
        houstonStore.orangesPrice = 0.87;
        double houstonStoreRevenue = ( houstonStore.applesSold * houstonStore.applesPrice) + (houstonStore.orangesSold * houstonStore.orangesPrice);
        System.out.println("The Houston store revenue is: " + houstonStoreRevenue);

        seattleStore.applesSold = 765;
        seattleStore.applesPrice = 0.86;
        seattleStore.orangesSold = 842;
        seattleStore.orangesPrice = 0.91;
        double seattleStoreRevenue = (seattleStore.applesSold * seattleStore.applesPrice) + (seattleStore.orangesSold * seattleStore.orangesPrice);
        System.out.println("The Seattle store revenue is: " + seattleStoreRevenue);

        orlandoStore.applesSold = 402;
        orlandoStore.applesPrice = 0.79;
        orlandoStore.orangesSold = 398;
        orlandoStore.orangesPrice = 0.77;
        double orlandoStoreRevenue = (orlandoStore.applesSold * orlandoStore.applesPrice) + (orlandoStore.orangesSold * orlandoStore.orangesPrice );
        System.out.println("The Orlando store revenue is: " + orlandoStoreRevenue);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...