我试图在我的库存类中调用我的游戏类。 Java的

时间:2017-04-28 20:13:53

标签: java oop

库存类

这是我试图显示玩家在被叫

时所拥有的类
package textadventure;

public class inventory
{
public boolean rustedsword;
public boolean lamp;
public inventory() {
    if (lamp == true) {
        System.out.println("You have a Lamp");
    }
    else  if (rustedsword == true && lamp == true) {
        System.out.println("You have a Rusted Sword");
        System.out.println("You have a Lamp");
    }else if (rustedsword == true) {
        System.out.println("You have a Rusted Sword");
    } else{
        System.out.println("You have Nothing in your inventory");
    }
}

}

游戏课

这是拥有大部分信息的类

package textadventure;

import java.util.Scanner;

public class Story
{
public boolean isAlive;
public boolean rustedsword;
public boolean lamp;
public int hunger;
private inventory newinv;
public Scanner input = new Scanner(System.in);
public void thestory()  {
    inventory newinv = new inventory();
    System.out.println("You are in a dark room");
    System.out.println("There is a light switch, would you like to turn it on?");
    String choice1 = input.nextLine();
    if (choice1.contains("yes")) {
        System.out.println("The lights sparked to life for a second then went dead.");
        System.out.println("While The lights were on You saw a lamp in the corner");
        System.out.println("Would you like to pick it up?");
        String choice6 = input.nextLine();
        if (choice6.contains("yes")) {
            lamp = true;
            System.out.println("The Lamp Blazed to life, The little light and heat that it gives out rejuvinates your soul");
            System.out.println("Lamp added to inventory, You can press 9 to view your inventory.");
        }
    }
    System.out.println("You hear the sound of motion to your left");
    System.out.println("1: Go investigate the sound");
    System.out.println("2: Stay put");
    String choice2 = input.nextLine();
    if (choice2.contains("9")) {
        inventory();
    if (choice2.contains("1")) {
        System.out.println("You slowly walk towards the sound. You find a hallway along with a rusted sword.");
        System.out.println("Would you like to pick up the sword?");
        String choice3 = input.nextLine();
        if (choice3.contains("yes")) {
            rustedsword = true;
            System.out.println("You picked up the rusted sword, you can feel the evil this blade has slain");
            System.out.println("Sword Added to Inventory, You can press 9 to view your inventory");
        }
        System.out.println("You see a Beast that is at least 8 feet tall");
        if (choice3.contains("yes")) {
            System.out.println("1: Stabby Stabby");
            System.out.println("2: Throw The Sword");
        }
        System.out.println("3: Stand Frozen In Fear");
        String choice4 = input.nextLine();
        if (choice4.contains("1")) {
            hunger = 9999;
            isAlive = false;
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("The Sword wasnt long enough...");
            System.out.println("OH NO! YOU DIED");

            System.out.println("                    ███████████████████████████");
            System.out.println("                    ███████▀▀▀░░░░░░░▀▀▀███████");
            System.out.println("                    ████▀░░░░░░░░░░░░░░░░░▀████");
            System.out.println("                    ███│░░░░░░░░░░░░░░░░░░░│███");
            System.out.println("                    ██▌│░░░░░░░░░░░░░░░░░░░│▐██");
            System.out.println("                    ██░└┐░░░░░░░░░░░░░░░░░┌┘░██");
            System.out.println("                    ██░░└┐░░░░░░░░░░░░░░░┌┘░░██");
            System.out.println("                    ██░░┌┘▄▄▄▄▄░░░░░▄▄▄▄▄└┐░░██");
            System.out.println("                    ██▌░│██████▌░░░▐██████│░▐██");
            System.out.println("                    ███░│▐███▀▀░░▄░░▀▀███▌│░███");
            System.out.println("                    ██▀─┘░░░░░░░▐█▌░░░░░░░└─▀██");
            System.out.println("                    ██▄░░░▄▄▄▓░░▀█▀░░▓▄▄▄░░░▄██");
            System.out.println("                    ████▄─┘██▌░░░░░░░▐██└─▄████");
            System.out.println("                    █████░░▐█─┬┬┬┬┬┬┬─█▌░░█████");
            System.out.println("                   ████▌░░░▀┬┼┼┼┼┼┼┼┬▀░░░▐████");
            System.out.println("                   █████▄░░░└┴┴┴┴┴┴┴┘░░░▄█████");
            System.out.println("                    ███████▄░░░░░░░░░░░▄███████");
            System.out.println("                    ██████████▄▄▄▄▄▄▄██████████");
            System.out.println("");                  

        }
        if (choice4.contains("3")) {
            hunger = 9999;
            isAlive = false;
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("A Vicious Beast Leaps toward you and you feel its claws rip and tear through");
            System.out.println("your chest!OH NO! YOU DIED");

            System.out.println("                    ███████████████████████████");
            System.out.println("                    ███████▀▀▀░░░░░░░▀▀▀███████");
            System.out.println("                    ████▀░░░░░░░░░░░░░░░░░▀████");
            System.out.println("                    ███│░░░░░░░░░░░░░░░░░░░│███");
            System.out.println("                    ██▌│░░░░░░░░░░░░░░░░░░░│▐██");
            System.out.println("                    ██░└┐░░░░░░░░░░░░░░░░░┌┘░██");
            System.out.println("                    ██░░└┐░░░░░░░░░░░░░░░┌┘░░██");
            System.out.println("                    ██░░┌┘▄▄▄▄▄░░░░░▄▄▄▄▄└┐░░██");
            System.out.println("                    ██▌░│██████▌░░░▐██████│░▐██");
            System.out.println("                    ███░│▐███▀▀░░▄░░▀▀███▌│░███");
            System.out.println("                    ██▀─┘░░░░░░░▐█▌░░░░░░░└─▀██");
            System.out.println("                    ██▄░░░▄▄▄▓░░▀█▀░░▓▄▄▄░░░▄██");
            System.out.println("                    ████▄─┘██▌░░░░░░░▐██└─▄████");
            System.out.println("                    █████░░▐█─┬┬┬┬┬┬┬─█▌░░█████");
            System.out.println("                   ████▌░░░▀┬┼┼┼┼┼┼┼┬▀░░░▐████");
            System.out.println("                   █████▄░░░└┴┴┴┴┴┴┴┘░░░▄█████");
            System.out.println("                    ███████▄░░░░░░░░░░░▄███████");
            System.out.println("                    ██████████▄▄▄▄▄▄▄██████████");
            System.out.println("");                  
            {}
               if (choice4.contains("2")) {
                    System.out.println("The Beast falls to the ground dead.");
                    System.out.println("You see light and the outline of a small village");
                    System.out.println("Do you want to greet the villagers?");
                    String choice5 = input.nextLine();
                    choice5 = input.nextLine();
                    if (choice5.contains("yes")) {
                        System.out.println("They greet you kindly");
                        System.out.println("YOU WON");

                    }
                    if (choice5.contains("no")) {
                        hunger = 9999;
                        isAlive = false;
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("");
                        System.out.println("A Vicious Beast Leaps toward you and you feel its claws rip and tear through");
                        System.out.println("your chest!OH NO! YOU DIED");

                        System.out.println("                    ███████████████████████████");
                        System.out.println("                    ███████▀▀▀░░░░░░░▀▀▀███████");
                        System.out.println("                    ████▀░░░░░░░░░░░░░░░░░▀████");
                        System.out.println("                    ███│░░░░░░░░░░░░░░░░░░░│███");
                        System.out.println("                    ██▌│░░░░░░░░░░░░░░░░░░░│▐██");
                        System.out.println("                    ██░└┐░░░░░░░░░░░░░░░░░┌┘░██");
                        System.out.println("                    ██░░└┐░░░░░░░░░░░░░░░┌┘░░██");
                        System.out.println("                    ██░░┌┘▄▄▄▄▄░░░░░▄▄▄▄▄└┐░░██");
                        System.out.println("                    ██▌░│██████▌░░░▐██████│░▐██");
                        System.out.println("                    ███░│▐███▀▀░░▄░░▀▀███▌│░███");
                        System.out.println("                    ██▀─┘░░░░░░░▐█▌░░░░░░░└─▀██");
                        System.out.println("                    ██▄░░░▄▄▄▓░░▀█▀░░▓▄▄▄░░░▄██");
                        System.out.println("                    ████▄─┘██▌░░░░░░░▐██└─▄████");
                        System.out.println("                    █████░░▐█─┬┬┬┬┬┬┬─█▌░░█████");
                        System.out.println("                   ████▌░░░▀┬┼┼┼┼┼┼┼┬▀░░░▐████");
                        System.out.println("                   █████▄░░░└┴┴┴┴┴┴┴┘░░░▄█████");
                        System.out.println("                    ███████▄░░░░░░░░░░░▄███████");
                        System.out.println("                    ██████████▄▄▄▄▄▄▄██████████");
                        System.out.println("");                  
                        if (choice2.contains("2")) {
                            hunger = 9999;
                            isAlive = false;
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("");
                            System.out.println("A Vicious Beast Leaps toward you and you feel its claws rip and tear through");
                            System.out.println("your chest!OH NO! YOU DIED");

                            System.out.println("                    ███████████████████████████");
                            System.out.println("                    ███████▀▀▀░░░░░░░▀▀▀███████");
                            System.out.println("                    ████▀░░░░░░░░░░░░░░░░░▀████");
                            System.out.println("                    ███│░░░░░░░░░░░░░░░░░░░│███");
                            System.out.println("                    ██▌│░░░░░░░░░░░░░░░░░░░│▐██");
                            System.out.println("                    ██░└┐░░░░░░░░░░░░░░░░░┌┘░██");
                            System.out.println("                    ██░░└┐░░░░░░░░░░░░░░░┌┘░░██");
                            System.out.println("                    ██░░┌┘▄▄▄▄▄░░░░░▄▄▄▄▄└┐░░██");
                            System.out.println("                    ██▌░│██████▌░░░▐██████│░▐██");
                            System.out.println("                    ███░│▐███▀▀░░▄░░▀▀███▌│░███");
                            System.out.println("                    ██▀─┘░░░░░░░▐█▌░░░░░░░└─▀██");
                            System.out.println("                    ██▄░░░▄▄▄▓░░▀█▀░░▓▄▄▄░░░▄██");
                            System.out.println("                    ████▄─┘██▌░░░░░░░▐██└─▄████");
                            System.out.println("                    █████░░▐█─┬┬┬┬┬┬┬─█▌░░█████");
                            System.out.println("                   ████▌░░░▀┬┼┼┼┼┼┼┼┬▀░░░▐████");
                            System.out.println("                   █████▄░░░└┴┴┴┴┴┴┴┘░░░▄█████");
                            System.out.println("                    ███████▄░░░░░░░░░░░▄███████");
                            System.out.println("                    ██████████▄▄▄▄▄▄▄██████████");
                            System.out.println("");                  
                        }

                    }
                }
            }
        }
    }
 }
}

Runner类(有主要方法)

此类具有主要方法,用于运行游戏

package textadventure;

public class Runner
{

private int hunger;


private boolean isAlive;

public static void main(String[] args){

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     int bathroom = 0;
     int hunger = 0;
     int sleep = 0;
    Story yaboi = new Story();


        yaboi.thestory();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将生锈的灯和灯从Story类传递到库存类。 而且您仍然需要调整if条件的顺序。

package textadventure;

public class inventory {
    public inventory(boolean rustedsword, boolean lamp) {
        if (lamp == true) {
            System.out.println("You have a Lamp");
        } else if (rustedsword == true && lamp == true) {
            System.out.println("You have a Rusted Sword");
            System.out.println("You have a Lamp");
        } else if (rustedsword == true) {
            System.out.println("You have a Rusted Sword");
        } else {
            System.out.println("You have Nothing in your inventory");
        }
    }

}