Java:来自另一个类的方法无法解决

时间:2016-11-12 20:07:00

标签: java

我是java的新手。我创建了一个类的对象但是当我尝试从对象访问一个方法时,它说它无法解决。这两个课程在

之下
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;

    public gameLoop() {
    }

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

    public void loop() {
        while (true) {
            System.out.println("Turn: "+turn);
            ***System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());***
            input.nextLine();
            turn++;
        }
    }
}

当我尝试从安装类

打印变量时,gameLoop类中出现错误

1 个答案:

答案 0 :(得分:1)

scala> Array("11","112","as2") res0: Array[String] = Array(11, 112, as2) scala> "['"+res0.mkString("','")+"']" res1: String = ['11','112','as2'] 移到setup setupGov = new setup();函数之外,将其放在main

我强烈建议你找一个好的编程基础课程,那里有很多