gpt4 book ai didi

Java:无法解析另一个类的方法

转载 作者:行者123 更新时间:2023-12-01 09:15:33 24 4
gpt4 key购买 nike

我是java新手。我创建了一个类的对象,但是当我尝试从该对象访问方法时,它说无法解析。下面是 2 个类

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 类中发生错误

最佳答案

移动setup setupGov = new setup();main函数,将其放在 static int turn = 0; 的正下方

我强烈建议您找到一个好的编程基础类(class),那里有很多

关于Java:无法解析另一个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40567124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com