为什么我在Java中得到NaN或null? 公共类主{ 公共静态void main(字符串[]args){ GroceryStore houstonStore=新杂货店(); GroceryStore orlandoStore=新杂货店(); GroceryStore seattleStore=新杂货店(); double houstonStore收入=(houstonStore.AppleSold*houstonStore.applesPrice)+(houstonStore.OrangeSold*houstonStore.orangesPrice); double OrlandostoreEvenue=(orlandoStore.AppleSold*orlandoStore.applesPrice)+(orlandoStore.OrangeSold*orlandoStore.orangesPrice); 双座商店收入=(seattleStore.AppleSold*seattleStore.applesPrice)+(seattleStore.OrangeSold*seattleStore.orangesPrice); houstonStore.applesSold=534; houstonStore.applesPrice=0.99; houstonStore.orangesSold=429; houstonStore.orangesPrice=0.87; System.out.println(“休斯顿商店收入为:“+houstonStoreRevenue”); seattleStore.applesSold=765; seattleStore.applesPrice=0.86; seattleStore.orangesold=842; seattleStore.orangesPrice=0.91; System.out.println(“西雅图商店收入为:“+seattleStoreRevenue”); orlandoStore.applesSold=402; orlandoStore.applesPrice=0.79; orlandoStore.orangesSold=398; orlandoStore.orangesPrice=0.77; System.out.println(“奥兰多商店的收入是:“+Orlandostreevenue”); } }

为什么我在Java中得到NaN或null? 公共类主{ 公共静态void main(字符串[]args){ GroceryStore houstonStore=新杂货店(); GroceryStore orlandoStore=新杂货店(); GroceryStore seattleStore=新杂货店(); double houstonStore收入=(houstonStore.AppleSold*houstonStore.applesPrice)+(houstonStore.OrangeSold*houstonStore.orangesPrice); double OrlandostoreEvenue=(orlandoStore.AppleSold*orlandoStore.applesPrice)+(orlandoStore.OrangeSold*orlandoStore.orangesPrice); 双座商店收入=(seattleStore.AppleSold*seattleStore.applesPrice)+(seattleStore.OrangeSold*seattleStore.orangesPrice); houstonStore.applesSold=534; houstonStore.applesPrice=0.99; houstonStore.orangesSold=429; houstonStore.orangesPrice=0.87; System.out.println(“休斯顿商店收入为:“+houstonStoreRevenue”); seattleStore.applesSold=765; seattleStore.applesPrice=0.86; seattleStore.orangesold=842; seattleStore.orangesPrice=0.91; System.out.println(“西雅图商店收入为:“+seattleStoreRevenue”); orlandoStore.applesSold=402; orlandoStore.applesPrice=0.79; orlandoStore.orangesSold=398; orlandoStore.orangesPrice=0.77; System.out.println(“奥兰多商店的收入是:“+Orlandostreevenue”); } },java,object,Java,Object,我不太明白为什么我会得到下面的答案 ***** The Houston store revenue is: 0.0 The Seattle store revenue is: 0.0 The Orlando store revenue is: 0.0 您正在引用尚未声明的属性。试试这个 public class Main { public static void main(String[] args) { GroceryStore houstonStore = ne

我不太明白为什么我会得到下面的答案

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


您正在引用尚未声明的属性。试试这个

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

默认情况下,
GroceryStore
对象具有哪些值?因为您在设置这些值之前正在执行计算。默认值可能为零。(另一方面,您的对象还应该公开setter和getter,而不是允许使用代码直接访问值。这还允许您在对象本身上编写一个方法来执行计算,因此您不必重复该逻辑,只需查询对象即可。)请包括所有相关代码,以便其他人能够正确回答您的问题。在本例中,
GroceryStore
类丢失。我很困惑,为什么标题上写着“NaN或null”,但您的代码不生成这两个,而是生成
0
(零)。Nan或null在哪里?
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);
    }
}