为什么我在我的Java代码中只得到了数组的最后一个对象?

huangapple go评论73阅读模式
英文:

Why do I get only the last object of an array in my Java code?

问题

我不明白为什么在执行代码后,即使moneyAmount的值为150或更高,也只显示数组drinksRange中最后一个对象water

public class Main {
    public static void main(String[] args) {
        System.out.println("咖啡机");

        Drink[] drinksRange = new Drink[4];

        drinksRange[0] = new Drink(Drink.name = "cappuccino", Drink.price = 150);
        drinksRange[1] = new Drink(Drink.name = "espresso", Drink.price = 80);
        drinksRange[2] = new Drink(Drink.name = "milk", Drink.price = 50);
        drinksRange[3] = new Drink(Drink.name = "water", Drink.price = 20);

        System.out.print("插入钱币金额: ");
        int moneyAmount = new Scanner(System.in).nextInt();

        for (Drink i : drinksRange) {
            if (moneyAmount >= i.price) {
                System.out.println("您可以购买" + i.name);
            }
        }
    }

    public class Drink {
        static String name;
        static int price;

        public Drink(String drinkName, int drinkPrice) {
            name = drinkName;
            price = drinkPrice;
            System.out.println(name + " - " + price);
        }
    }
}

结果如下:

咖啡机
cappuccino - 150
espresso - 80
milk - 50
water - 20
插入钱币金额: 150
您可以购买water
您可以购买water
您可以购买water
您可以购买water
英文:

I don't understand why only the last object water of array drinksRange is shown after executing the code even if the value of moneyAmount is 150 or more

 public class Main {
 public static void main(String[] args) {
    System.out.println("Coffee-machine");

    Drink[] drinksRange = new Drink [4];

    drinksRange[0] = new Drink(Drink.name = "cappuccino", Drink.price = 150);
    drinksRange[1] = new Drink(Drink.name = "espresso", Drink.price = 80);
    drinksRange[2] = new Drink(Drink.name = "milk", Drink.price = 50);
    drinksRange[3] = new Drink(Drink.name = "water", Drink.price = 20);

    System.out.print("Insert money: ");
    int moneyAmount = new Scanner(System.in).nextInt();

        for (Drink i : drinksRange) {
            if (moneyAmount >= i.price) {
                System.out.println("You can buy " + i.name);
            }
        }
}

public class Drink {
  static String name;
  static int price;

 public Drink (String drinkName, int drinkPrice) {
  name = drinkName;
  price = drinkPrice;
  System.out.println(name + " - " + price);
 }
}

As a result I get this:

Coffee-machine
cappuccino - 150
espresso - 80
milk - 50
water - 20
Insert money: 150
You can buy water
You can buy water
You can buy water
You can buy water

答案1

得分: 1

static String name;
static int price;

由于这些字段是 static 的,因此在调用 Drink 类的任何静态属性时,最后一个值将是所有对象的最终值,因此 ArrayList 中只包含饮料 "water" 和价格 20。

请查看下面修改后的代码:

import java.util.Scanner;

public class SOTest {

    public static void main(String[] args) {
        System.out.println("Coffee-machine");

        Drink[] drinksRange = new Drink[4];

        drinksRange[0] = new Drink("cappuccino", 150);
        drinksRange[1] = new Drink("espresso", 80);
        drinksRange[2] = new Drink("milk", 50);
        drinksRange[3] = new Drink("water", 20);

        System.out.print("Insert money: ");
        int moneyAmount = new Scanner(System.in).nextInt();

        for (Drink i : drinksRange) {
            if (moneyAmount >= i.price) {
                System.out.println("You can buy " + i.name);
            }
        }
    }
}
class Drink {
    String name;
    int price;

    public Drink(String drinkName, int drinkPrice) {
        name = drinkName;
        price = drinkPrice;
        System.out.println(name + " - " + price);
    }
}

输出:

Coffee-machine
cappuccino - 150
espresso - 80
milk - 50
water - 20
Insert money: 200
You can buy cappuccino
You can buy espresso
You can buy milk
You can buy water
英文:

Your fields

static String name;
static int price;

are static hence the last value will be the final value for all objects when u are calling Drink.[any static property] hence the arraylist only contains the drink as water and value 20.

Check the modified code below:

import java.util.Scanner;

public class SOTest {

    public static void main(String[] args) {
        System.out.println("Coffee-machine");

        Drink[] drinksRange = new Drink [4];

        drinksRange[0] = new Drink("cappuccino", 150);
        drinksRange[1] = new Drink("espresso",80);
        drinksRange[2] = new Drink("milk", 50);
        drinksRange[3] = new Drink("water", 20);

        System.out.print("Insert money: ");
        int moneyAmount = new Scanner(System.in).nextInt();

        for (Drink i : drinksRange) {
            if (moneyAmount >= i.price) {
                System.out.println("You can buy " + i.name);
            }
        }
    }
}
class Drink {
    String name;
    int price;

    public Drink (String drinkName, int drinkPrice) {
        name = drinkName;
        price = drinkPrice;
        System.out.println(name + " - " + price);
    }
}

Output:

Coffee-machine
cappuccino - 150
espresso - 80
milk - 50
water - 20
Insert money: 200
You can buy cappuccino
You can buy espresso
You can buy milk
You can buy water

答案2

得分: 1

static int price 这会导致那个错误。
每当你使用 static 关键字时,它意味着只有一个副本。因此,会选择“最新”的值,而在你的情况下是 Drink.price = 20
例如:

Line 1    drinksRange[0] = new Drink(Drink.name = "cappuccino", Drink.price = 150)
Line 2    drinksRange[1] = new Drink(Drink.name = "espresso", Drink.price = 80)
Line 3    drinksRange[2] = new Drink(Drink.name = "milk", Drink.price = 50)
Line 4    drinksRange[3] = new Drink(Drink.name = "water", Drink.price = 20)

毫无疑问,这会创建一个 Drink 的实例,所以当执行第 1 行时,drinksRange[0] 在内存中有其自己的空间,但是类中的 static String name, static int price 成员在堆内有独立的内存。
因此,当执行第 1 行时,namecappuccinoprice150
当执行第 2 行时,nameespressoprice80
当执行第 3 行时,namemilkprice50
当执行第 4 行时,namewaterprice20

因此,为了得到你想要的输出,只需删除那个 static 关键字,它就会正常工作。

英文:

static int price this is causing that bug<br>
whenever you write a static keyword it means there's only one copy of it. So the lastest value is picked which in your case is Drink.price = 20
eg.

Line 1    drinksRange[0] = new Drink(Drink.name = &quot;cappuccino&quot;, Drink.price = 150)
Line 2    drinksRange[1] = new Drink(Drink.name = &quot;espresso&quot;, Drink.price = 80)
Line 3    drinksRange[2] = new Drink(Drink.name = &quot;milk&quot;, Drink.price = 50)
Line 4    drinksRange[3] = new Drink(Drink.name = &quot;water&quot;, Drink.price = 20)

No doubt this makes an instance of Drink, So line 1 when executed drinksRange[0] has its own space in memory but the members in the class static String name,
static int price
have independent memory in heap<br>
so when line 1 is executed name as cappuccino and price as 150<br>
when line 2 is executed name as espresso and price as 80<br>
when Line 3 is executed name as milk and price as 50<br>
when line 4 is executed name as water and price as 20<br>

So in order to have the output, you wanted just remove that static keyword and it will work fine.

答案3

得分: 1

静态变量在类加载时只在类区域中分配一次内存。

static String name;
static int price;

在你的情况下,你不能把它放在你的代码中,它会取数组中的最后一个值“water”。

按照下面的方式放置,它就会正确工作:

String name;
int price;
英文:

The static variable gets memory only once in the class area at the time of class loading.

static String name;
static int price;

In your case you can't put it in your code it will take the last value "water" you have on your array

Put it like this then it will work correctly 为什么我在我的Java代码中只得到了数组的最后一个对象?

String name;
int price;

huangapple
  • 本文由 发表于 2020年10月9日 03:37:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64269511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定