Java objects of classes not returning the same values

你说的曾经没有我的故事 提交于 2019-12-25 07:59:11

问题


I am creating an object of a class from 2 separate classes and both objects are returning different values for the same method. I suspect it may be an issue with the while loop but here are the classes. The main class works, the setup class is the class that is being turned into and object and the game loop class has the object that doesn't return the right values. it returns the values defined at the beginning of setup and not the modified versions.

import java.util.Scanner;

public class MainClass {
    static Scanner input = new Scanner(System.in);
    //String x = input.nextLine();

    public static void main(String[] args)
    {
        setup setupGov = new setup();
        gameLoop gameLoop = new gameLoop();

        setupGov.statsSetup();
        System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());

        gameLoop.loop();

    }
}

import java.util.Scanner;

public class setup {
    static Scanner input = new Scanner(System.in);

    String goverment;
    int happyness;
    double money;
    int population = 1000000;


    public setup()
    {
    }

    public void statsSetup()
    {
        System.out.println("Choose a goverment: 1. democracy 2. monarchy 3. dictatorship");
        goverment = input.nextLine();

        if (goverment.equals("1"))
        {
            happyness = 75;
            money = 250000.0;

        }
        else if (goverment.equals("2"))
        {
            happyness = 50;
            money = 500000.0;
        }
        else if (goverment.equals("3"))
        {
            happyness = 25;
            money = 750000.0;
        }
        else
        {
            System.out.println("ENTER A VALID VALUE");
        }
    }
    public int getHappyness()
    {
        return happyness;
    }
    public double getMoney()
    {
        return money;
    }
    public int getPopulation()
    {
        return population;
    }
}

import java.util.Scanner;

public class gameLoop 
{
    static Scanner input = new Scanner(System.in);

    static int turn = 0;
    int happyness;
    double money;
    int population;

    public gameLoop()
    {
    }

    setup setupGov = new setup();

    public static void main(String[] args)
    {

    }

    public void loop() 
    {
        while (true)
        {
            System.out.println("Turn: "+turn);
            input.nextLine();
            turn++;
        }
    }

}

回答1:


You are creating two different instances of class setup. One is created directly in main function and other is created in gameLoop object. They do not share their attributes so methods may return different value. Every time you use 'new' operator, a new instance of class is created with it's own attributes (only static member are shared since static member belongs to class instead of instances). If you want to have same instances you could write:

 public class gameLoop 
 {
    static Scanner input = new Scanner(System.in);

    static int turn = 0;
    int happyness;
    double money;
    int population;

    public gameLoop(setup setupGov) 
    {
       this.setupGov = setupGov;
    }

    setup setupGov;

    public static void main(String[] args)
    {

    }

    public void loop() 
    {
        while (true)
        {
            System.out.println("Turn: "+turn);
            input.nextLine();
            turn++;
        }
    }

}

And in main:

public class MainClass {
    static Scanner input = new Scanner(System.in);
    //String x = input.nextLine();

    public static void main(String[] args)
    {
        setup setupGov = new setup();
        gameLoop gameLoop = new gameLoop(setupGov);

        setupGov.statsSetup();
        System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());

        gameLoop.loop();

    }
}

Now both of objects setupGov will be the same instance.

Please note: It is good practice to have class name written with capitalized first letter eg. GameLoop instead of gameLoop




回答2:


I don't really understand what you're trying to do or what the question is, but in your main class you have an object with the same exact name of the class.

gameLoop gameLoop = new gameLoop();

I don't know if that's the exact cause of your problem, but I'm almost sure that that isn't supposed to be like that.



来源:https://stackoverflow.com/questions/40568063/java-objects-of-classes-not-returning-the-same-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!