为什么没有存储对象值?

时间:2016-01-30 20:50:59

标签: java oop methods constructor

***在bottle3将.set()设置为0之后,所有值似乎都设置为零。甚至拿了瓶子3.set(0);驱动程序的位和其他瓶子对象似乎仍然失去了应该设置的值。

import java.util.Scanner;
   test driver for the Bottle class
public class BottleDriver extends Bottle
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
    int x;
    Bottle bottle1 = new Bottle();
    Bottle bottle2 = new Bottle();
    Bottle bottle3 = new Bottle();
    Bottle bottle4 = new Bottle();
    Bottle bottle5 = new Bottle();

    System.out.println("please enter a number for bottle1:");
    bottle1.read();
    System.out.println("Bottle1 is this value " + bottle1.marbles + ".");
    System.out.println("Please enter a number for bottle2:");
    bottle2.read();
    System.out.println("Bottle2 is this value " + bottle2.marbles + ".");
    bottle3.set(0);
    System.out.println("Bottle3 is set to " + bottle3.marbles + ".");
    bottle3 = bottle3.add(bottle1);
    System.out.println(bottle3.marbles);
    bottle3 = bottle3.add(bottle2);
    bottle3 = bottle3.divide(2);
    System.out.println("The 2 bottle average is: " + bottle3 + ".");
    System.out.print("Subtracting bottle1 from bottle2 is: " );
    bottle3 = bottle2.subtract(bottle1);
    System.out.println( bottle3);
    bottle3 = bottle2.divide(bottle1);
    System.out.println("Dividing bottle2 with bottle1 is: " + bottle3 + ".");
    if (bottle1.equals(bottle2))
    {
        System.out.println("Bottle1 and bottle2 are equal.");
    }
    else
    {
        System.out.println("Bottle1 and bottle2 are not equal.");
    }
    System.out.println("Bottle4 is now given the value of 10 with the set()method.");
            bottle4.set(10);
            System.out.println("The value of bottle4 is " + bottle4 + ".");
            System.out.println("Bottle4 is now multiplied with bottle1. The value is placed in bottle5.");
                    bottle5 = bottle1.multiply(bottle4);
                    System.out.println("The value of bottle5 is " + bottle5 + ".");
                    System.out.println("Enter an integer to add to the value bottle1 has.");
                    System.out.println("The sum will be put in bottle3.");
                    x = scan.nextInt();
                    bottle3 = bottle1.add(x);
                    System.out.println("Adding your number " + x +
                            " to bottle1 gives a new Bottle with " + bottle3 + " in it.");
                    System.out.print("Adding the number " + bottle2 + " which is the number" +
                            " in bottle2 to the\nnumber in ");
                    bottle2 = bottle1.add(bottle2);
                    System.out.println("bottle1 which is " + bottle1 +" gives " + bottle2 + ".");
}
}

// ***瓶子类

import java.util.Scanner;
public class Bottle
{
public static final int MAX = 75;
public static final int MIN = 0;
public static int marbles;

Bottle()
{
    marbles = 0;
}

public int get()
{
    return this.marbles;
}

public void read() {

    Scanner keyboard = new Scanner(System.in);
    marbles = keyboard.nextInt();
    set(marbles);

}


public void set(int marbles) 
{   

    if(marbles > MAX || marbles < MIN)
    {
        System.out.println("Below or exceeded capacity of the bottle.");
        System.exit(0);
    }
    else
    {
        this.marbles = marbles;
    }

}


public Bottle add(Bottle bottle)
{
    Bottle newBottle = new Bottle();
    newBottle.set(newBottle.marbles + bottle.marbles);
    return newBottle;
}

public int subtract(int bottle)
{

}

public int multiply(int bottle)
{

}

public int divide(int bottle)
{

}



}

1 个答案:

答案 0 :(得分:0)

问题是弹珠是static字段。静态字段在类的所有实例之间共享,以帮助节省内存。删除static注释,它应该可以解决您的问题。

public static int marbles;