java—如何检查和使用来自不同方法的输入

vnzz0bqm  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(281)

我正在制作一个游戏,在游戏结束时,我希望它按用户输入的名称调用用户,这是我的代码。

private static final Scanner console = new Scanner(System.in);

public static void main(String[] args) {// follow the prompts.//

    System.out.println("Hello user! what is your name? ");
    String Name = console.nextLine();
    System.out.println("Really? " + Name + " is too weird to be a real name.");
    confirmation();
    Mascot();
    System.out.println("Thank you for playing the demo");

    console.close();
}

public static void confirmation() {

    System.out.print("is that REALLY your name? (type Y/N) ");
    String yN = console.nextLine();
    String a = yN;
    if (a.toLowerCase().contains("y")) {
        System.out.println("I still dont belive you, so you will have to answer 3 riddles before you can continue to the game");
    } else {
        calledIt();
    }

}

public static void calledIt() {

    System.out.println("I knew it!");
    System.out.print("whats your real name? ");
    String realName = console.nextLine();
    System.out.println(
            "" + realName + " sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");

}

public static boolean Mascot() {

    System.out.println("what Is our school mascot?");
    String b = console.nextLine();
    if (b.toLowerCase().contains("tiger")) {
        System.out.println("Good, next riddle.");
        System.out.println("What runs around the whole yard without moving?");
        String c = console.nextLine();
        if (c.toLowerCase().contains("fence")) {
            System.out.println("Good, next riddle.");
            System.out.println("What goes on four feet in the morning, two feet at noon, and three feet in the evening? ");
            String d = console.nextLine();
            if (d.toLowerCase().contains("man")) {
                System.out.println("You, have sucsefully passed the third riddle");
                return true;
            } else {
                System.out.println("You have failed");
                return false;
            }

        } else {
            System.out.println("You have failed");
            return false;
        }
    } else {
        System.out.println("You have failed");
        return false;
    }
}

我想让它在最后打印用户名,你已经成功地通过了第三个谜题。但是它需要能够判断名字是否被保留,或者是否使用了这个序列。

public static void calledIt() {

    System.out.println("I knew it!");
    System.out.print("whats your real name? ");
    String realName = console.nextLine();
    System.out.println(
            "" + realName + " sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");

}

如果它已经被激活,它需要使用新的名称。

50pmv0ei

50pmv0ei1#

您可以进行以下更改:

public static void main(String[] args) { // follow the prompts.//
    System.out.println("Hello user! What is your name? ");
    String name = console.nextLine();
    System.out.println("Really? " + name + " is too weird to be a real name.");
    System.out.print("Is that REALLY your name? (type Y/N) ");
    String yN = console.nextLine();
    String a = yN;
    if (a.toLowerCase().contains("y")) {
        System.out.println("I still don't believe you, so you will have to answer 3 riddles before you can continue to the game");
    } else {
        System.out.println("I knew it!");
        System.out.print("Whats your real name? ");
        name = console.nextLine();
        System.out.println(
            "" + name + " sounds like a real one, but you lied the first time so you will need to answer riddles 3 to continue to the game");
    }

    mascot(name);
    System.out.println("Thank you for playing the demo");

    console.close();
}

public static boolean mascot(String name) {

    System.out.println("what Is our school mascot?");
    String b = console.nextLine();
    if (b.toLowerCase().contains("tiger")) {
        System.out.println("Good, next riddle.");
        System.out.println("What runs around the whole yard without moving?");
        String c = console.nextLine();
        if (c.toLowerCase().contains("fence")) {
            System.out.println("Good, next riddle.");
            System.out.println("What goes on four feet in the morning, two feet at noon, and three feet in the evening? ");
            String d = console.nextLine();
            if (d.toLowerCase().contains("man")) {
                System.out.println(name + ", you have successfully passed the third riddle");
                return true;
            } else {
                System.out.println("You have failed");
                return false;
            }

        } else {
            System.out.println("You have failed");
            return false;
        }
    } else {
        System.out.println("You have failed");
        return false;
    }
}
lb3vh1jj

lb3vh1jj2#

可以像这样将变量传递到confirmation()和calledit()中

public static void main(String[] args) {// follow the prompts.//

    System.out.println("Hello user! what is your name? ");
    String Name = console.nextLine();
    System.out.println("Really? " + Name + " is too weird to be a real name.");
    confirmation(Name);
    Mascot();
    System.out.println("Thank you for playing the demo");

    console.close();
}

public static void confirmation(String name) {

        System.out.print("is that REALLY your name? (type Y/N) ");
        String yN = console.nextLine();
        String a = yN;
        if (a.toLowerCase().contains("y")) {
            System.out.println("I still dont belive you, so you will have to answer 3 riddles before you can continue to the game");
        } else {
            calledIt(name);
        }

    }

     public static void calledIt(String realName){
            System.out.println("I knew it!");
            System.out.print("whats your real name? ");
            System.out.println(
                    "" + realName + " sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");

        }
eh57zj3b

eh57zj3b3#

将calledit()的返回类型更改为string并从此方法返回realname
将confirmation()的返回类型更改为string。初始化字符串(字符串名称=null)。在else部分,将calledit()返回的值赋给这个字符串(string name=calledit())。返回名称。
在main中,如果confirmation()返回的值不为null,则使用此新值更新name。
将名称作为输入传递给吉祥物方法。为此,必须更新mascot方法以接受字符串作为输入。

相关问题