簡體   English   中英

為什么不存儲對象值?

[英]Why are object values not being stored?

***將bottle3的.set()設置為0后,所有值似乎都設置為零。 甚至拿了bottle3.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)
{

}



}

問題是大理石是static 靜態字段在類的所有實例之間共享,以幫助節省內存。 刪除static注釋,它可以解決您的問題。

public static int marbles;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM