我的文字冒险游戏中的方法和类不了解我的错误

时间:2019-03-25 13:19:42

标签: java java-8

类和源文件称为textadv1

错误消息是“无法访问类型为textadv1的封闭实例。必须使用类型为textadv1的封闭实例来限定分配,例如x.new(A),其中x是textadv1的实例”

代码“导致”错误出现在这里:room startRoom = new room(); {

import java.util.*;
/**
 * 
 */

/**
 * @author savio
 *
 */
public class textadv1 {

    /**
     * @param args
     */
    //FIRST: DICE ROLLS
    public class Dice {
        private final Random random = new Random();

        public int roll(int max) {
            return 1 + random.nextInt(max);
        }

        public int roll6() {
            return roll(6);
        }

        public int rollAtk() {
            return roll(4) + roll(4);
        }
    }
    //EXITS
    //NEXT: ROOMS!!!
    public class room{
        private int roomid;
        public String description;
        public boolean combat = false;
        public String exitn = "You have walked into the wall";
        public String exite = "You have walked into the wall";
        public String exits = "You have walked into the wall";
        public String exitw = "You have walked into the wall";


        public room(){
            roomid = 0;
            description = "";
        }
        public void setroomid(int i){
            this.roomid = i;
        }
        public int getroomid() {
            return this.roomid;
        }
        public void setdescription(String s) {
            this.description = s;
        }
        public String getdescription() {
            return this.description;
        }
        public void setexitn(String s) {
            exitn = s;
        }
        public void setexite(String s) {
            exite = s;
        }
        public void setexits(String s) {
            exits = s;
        }
        public void setexitw(String s) {
            exitw = s;
        }
    }
    //NAVI
    public static void play(room r) {
        System.out.println(r.getdescription());
    }
    //MAP

    //MOVEMENT

    public static void main(String[] args) {
        room startRoom = new room();{
            startRoom.setroomid(0);
            startRoom.setexitn("You go north");
            startRoom.setdescription("You awaken in a cold, dark room. \nThe grey brick walls are damp, and the ceiling leaks. There is an exit to your North. To go north, input \"N\"");
        }
        room room0 = new room();{
        room0.setroomid(1);
        room0.setdescription("You are in a stone hallway. Your start point is to the South, there is a dark hallway to the North. There is a torch on the wall.");
        room0.setexits("You go south");
        }
        // TODO MAKE DICE, COMBAT, MOVEMENT, INVENTORY, MORALITY
        play(startRoom);
    }
}

4 个答案:

答案 0 :(得分:1)

您正在尝试从non-static块访问static类。

请提及您的房间教室为静态教室,这样就可以了

public static class room{

答案 1 :(得分:0)

这主要是由于您从静态上下文中进行了非静态引用所致。

您的main方法不是静态的,而您的rooms方法是静态的。 你可以
1.使rooms类为静态
2.将您的rooms类放在您的textadv1类之外(在这种情况下,删除公共访问说明符)

答案 2 :(得分:0)

您正在尝试从静态块访问非静态类。 Main是静态的,因此您的房间等级也应该是静态的

答案 3 :(得分:0)

在访问(非静态)内部类时,您需要具有一个封装外部类的实例。在这种情况下,在线83和89上没有该封闭实例。因此,您应该首先创建textadv1的实例,然后使用该实例创建room对象,如下所示: 在第83行上添加textadv1 txtadv = new textadv1();,然后使用 room startRoom = txtadv.new room();代替room startRoom = new room();

,并且也使用room room0 = txtadv.new room();代替room room0 = new room();