我正在制作一个游戏,在游戏结束时,我希望它按用户输入的名称调用用户,这是我的代码。
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");
}
如果它已经被激活,它需要使用新的名称。
3条答案
按热度按时间50pmv0ei1#
您可以进行以下更改:
lb3vh1jj2#
可以像这样将变量传递到confirmation()和calledit()中
eh57zj3b3#
将calledit()的返回类型更改为string并从此方法返回realname
将confirmation()的返回类型更改为string。初始化字符串(字符串名称=null)。在else部分,将calledit()返回的值赋给这个字符串(string name=calledit())。返回名称。
在main中,如果confirmation()返回的值不为null,则使用此新值更新name。
将名称作为输入传递给吉祥物方法。为此,必须更新mascot方法以接受字符串作为输入。