无法将游戏重置为上一轮JAVA

时间:2016-12-05 18:03:34

标签: java arrays loops methods

我制作了一个程序,要求用户提供他们的宠物名称,并将其状态分配给5轮。如果宠物太生气了就放下了。

我遇到的麻烦是允许用户在宠物死亡前重新开始游戏的早期阶段。

编译代码有助于说明问题。

任何帮助都将不胜感激。

import java.util.Random;
import java.util.Scanner;


class dinoo {
    public static void main(String[] p) {
        Pet p1 = new Pet();
        Pet p2 = new Pet();
        Scanner scanner = new Scanner(System.in);


        String petname;
        String species;
        String angerlevel;
        int thirst;
        int hunger;
        int irritability;

        explain();
        petname = askpetname();
        species = askpetspecies();
        int howmanyrounds = 5;
        for (int i = 0; i < howmanyrounds; i++) {
            int number = i + 1;
            String num = Integer.toString(number);
            print("Round " + number);
            String[] emotionalstate = newarray(5);

            thirst = thirstlevel(p1);
            hunger = hungerlevel(p1);
            irritability = irritabilitylevel(p1);
            angerlevel = anger(hunger, thirst, irritability);


            p1 = setpetname(p1, petname);
            p1 = setspecies(p1, species);
            p1 = setthirst(p1, thirst);
            p1 = setanger(p1, angerlevel);
            p1 = sethunger(p1, hunger);
            p1 = setangervalue(p1, irritability);

            print(petname + "'s thirst level is " + thirst + " , hunger level is " + hunger + ", irritability level is " + irritability + " and therefore emotional state is " + angerlevel);

            if (angerlevel.equals("DANGEROUS")) {
                print(petname + " is looking " + angerlevel + ", get out now! we will have to put " + petname + " down.");

                boolean continueEnd = petdead();
                if (continueEnd == false) {
                    System.exit(0);
                } else {
                    emotionalstate = wheretogo(emotionalstate[]);
                }

            } else if (angerlevel.equals("Serene")) {
                print("He looks  " + angerlevel + ". Seems in a good mood.");
            } else if (angerlevel.equals("Grouchy")) {
                print("You should give him a treat to cheer him up.");
            }

            whatdoyouwant(p1);
            print("Your pets emotion is now " + anger(getthirst(p1), gethungervalue(p1), getirritabilityvalue(p1)));

            emotionalstate[i] = anger(getthirst(p1), gethungervalue(p1), getirritabilityvalue(p1));
            print("Emotional state " + emotionalstate[i] + " has been saved!");


        }


        System.exit(0);

    }

    public static String[] wheretogo(String[] a) {
        Scanner scanner = new scanner;
        print("which level would you like to return to?");
        int number = scanner.nextInt();
        String level = a[number];
        return a;
    }

    public static boolean petdead() {
        Scanner scanner = new Scanner(System.in);
        print("Your pet has been killed. Would you like to continue? (True or False)");
        boolean yesorno = scanner.nextBoolean();
        return yesorno;
    }

    public static int inputint(String message) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(message);
        int answer = scanner.nextInt();
        return answer;
    }

    public static String[] newarray(int arraylength) {
        String[] emotionalstate = new String[arraylength];
        return emotionalstate;
    }

    //takes input from user to cheerup/feed/sing to the animal to lower values stored in setter/getter
    public static Pet whatdoyouwant(Pet p1) {
        Scanner scanner = new Scanner(System.in);
        print("what would you like to do to the pet? (treat/water/sing)");
        String ans = scanner.nextLine();
        Random ran = new Random();

        int reduction = ran.nextInt(6);

        if (ans.equalsIgnoreCase("treat")) {
            int hunger = gethungervalue(p1) - reduction;
            if (hunger < 0) {
                hunger = 0;
            }

            p1 = sethunger(p1, hunger);
            print("Your pets hunger has been reduced to:");
            printint(gethungervalue(p1));

        } else if (ans.equals("sing")) {
            int irrit = getirritabilityvalue(p1) - reduction;
            if (irrit < 0) {
                irrit = 0;
            }

            p1 = setirritability(p1, irrit);
            print("Your pets irritability hs been reduced to:");
            printint(getirritabilityvalue(p1));
        } else if (ans.equals("water")) {
            int thirst = getthirst(p1) - reduction;
            if (thirst < 0) {
                thirst = 0;
            }

            p1 = setthirst(p1, thirst);
            print("Your pets thirst is has been reduced to:");
            printint(getthirst(p1));
        } else {
            print("That action seems to agitate your pet, try something else before your pet becomes dangerous!");
        }
        return p1;
    }

    //GETTER METHOD
    public static String getpetname(Pet p) {
        return p.name;
    }

    public static String getspecies(Pet p) {
        return p.species;
    }

    public static String getanger(Pet p) {
        return p.anger;
    }

    public static int getthirst(Pet p) {
        return p.thirst;
    }

    public static int gethungervalue(Pet p) {
        return p.hunger;
    }

    public static int getangervalue(Pet p) {
        return p.angervalue;

    }

    public static int getirritabilityvalue(Pet p) {
        return p.irritability;
    }

    //SETTER METHOD
    public static Pet setangervalue(Pet p, int whatsangervalue) {
        p.angervalue = whatsangervalue;
        return p;
    }

    public static Pet setpetname(Pet p, String name) {
        p.name = name;
        return p;
    }

    public static Pet setspecies(Pet p, String petspecies) {
        p.species = petspecies;
        return p;
    }

    public static Pet setanger(Pet p, String howangry) {
        p.anger = howangry;
        return p;
    }

    public static Pet sethunger(Pet p, int howhungry) {
        p.hunger = howhungry;
        return p;
    }

    public static Pet setthirst(Pet p, int howthirsty) {
        p.thirst = howthirsty;
        return p;
    }

    public static Pet setirritability(Pet p, int howirritable) {
        p.irritability = howirritable;
        return p;
    }

    //Method printing out statement to explain functionality of program
    public static void explain() {

        print("The following program demonstrates use of user input by asking for pet name.");

        return;
    }

    //Method to ask the pet name
    public static String askpetname() {
        Scanner scanner = new Scanner(System.in);

        print("Name your dinosaur pet!");

        String petname = scanner.nextLine();
        return petname;
    }

    //Method to ask the pet species
    public static String askpetspecies() {
        Scanner scanner = new Scanner(System.in);

        print("What species is your pet?");
        String petspecies = scanner.nextLine();

        return petspecies;
    }

    //Randomly allocates thirst value 0-10
    public static int thirstlevel(Pet p1) {
        Random ran = new Random();

        int thirst = ran.nextInt(11);
        setthirst(p1, thirst);

        return thirst;
    }

    //Randomly Allocates hunger value 0-10
    public static int hungerlevel(Pet p1) {
        Random ran = new Random();

        int hunger = ran.nextInt(11);
        sethunger(p1, hunger);
        return hunger;
    }

    //randomly generates a irratibilty value
    public static int irritabilitylevel(Pet p1) {
        Random ran = new Random();

        int irritable = ran.nextInt(11);
        setirritability(p1, irritable);
        return irritable;
    }

    //Method calculates the anger value based on the thirst/hunger/irritability average
    public static String anger(int thirst, int hunger, int irritability) {
        int angerscore = (thirst + hunger + irritability) / 3;

        String temper;


        temper = Integer.toString(angerscore);

        if (angerscore <= 1) {
            temper = "Serene";
        } else if (angerscore <= 3) {
            temper = "Grouchy";
        } else if (5 < angerscore) {
            temper = "DANGEROUS";
        }


        return temper;
    }

    //HELPER PRINT METHOD
    public static String print(String message) {
        System.out.println(message);

        return message;
    }

    public static int printint(int message) {
        System.out.println(message);
        return message;
    }

}//END class dinoo


class Pet {
    String name;
    String species;
    int thirst;
    int hunger;
    String anger;
    int irritability;
    int angervalue;
} //END class pet

1 个答案:

答案 0 :(得分:0)

首先,您应该在提交问题之前查看代码中的编译错误。 请阅读评论中提供的链接,了解如何提交正确的问题。

我尝试编译您的代码,但它有以下错误:

java2.java:51: error: '.class' expected
                    emotionalstate = wheretogo(emotionalstate[]);
                                                               ^
java2.java:75: error: '(' or '[' expected
        Scanner scanner = new scanner;

这意味着在第51行,你正在传递emotionalstate[],这是情绪状态数组,而emotionstate本身就是一个数组,所以你要传递一个数组的数组。

所以只需将其更改为emotionalstate = wheretogo(emotionalstate)

即可

在第75行中,Scanner对象的初始化错误,将其更改为Scanner scanner = new Scanner();