gpt4 book ai didi

java - Scanner.nextLine();由于循环而在需要之前调用

转载 作者:行者123 更新时间:2023-11-30 07:39:10 25 4
gpt4 key购买 nike

我有一个扫描仪input,它由我的Player 类中的getName() 调用。

public static String getName()
{
System.out.println("Enter your character's name.");
String n = input.nextLine();
return n;
}

但是,当我使用循环来检查 RPG 类中是否有适当的玩家输入时,一切正常,直到我输入“n”或“N”代表

System.out.println("Confirm Character (Y/N)");
confirm = input.next();

循环的工作方式是,如果“n”或“N”,它会再次调用构造函数 public Player(String cName, int cStamina, int cDefense, int cStrength, int cAgility, int cIntellect)但是,因为这两个是 String 数据类型,并且构造函数首先调用名称,所以它要么将“n”或“N”识别为名称,要么完全跳过整个名称。我该如何解决这个问题?

import java.util.*;
public class RPG
{
static Scanner input = new Scanner(System.in);
private static String confirm;

public static void main(String[] args)
{
System.out.println("*~~~~~~~~~~~~~~*\n{ ~ Pinnacle ~ }\n*~~~~~~~~~~~~~~*");
for (int s = 0; s < 1; s++)
{

Player me = new Player(Player.getName(), Player.getStam(), Player.getDef(),
Player.getStr(), Player.getAgi(), Player.getInt());

for (int i = 0; i < 1; i++)
{
System.out.println("*~~~~~~~~~~~~~~*\n"+Player.name +
"\nStamina: " + Player.stamina +
"\nDefense: " + Player.defense +
"\nStrength: " + Player.strength +
"\nAgility: " + Player.agility +
"\nIntellect: " + Player.intellect +
"\n*~~~~~~~~~~~~~~*");
System.out.println("Confirm Character (Y/N)");
confirm = input.next();
if (confirm.equals("Y") || confirm.equals("y"))
System.out.println("Character Created!");
else if (confirm.equals("N") || confirm.equals("n"))
s--;
else
i--;
}
}
}
}

玩家等级如下

import java.util.*;

public class Player extends Characters
{

static Scanner input = new Scanner(System.in);

public Player(String cName, int cStamina, int cDefense, int cStrength, int cAgility, int cIntellect)
{


name = cName;
stamina = cStamina;
defense = cDefense;
strength = cStrength;
agility = cAgility;
intellect = cIntellect;
}
public static String getName()
{
System.out.println("Enter your character's name.");
String n = input.nextLine();
return n;
}
public static int getStam()
{
System.out.println("Enter your character's stamina.");
int s = input.nextInt();
return s;
}
public static int getDef()
{
System.out.println("Enter your character's defense.");
int d = input.nextInt();
return d;
}
public static int getStr()
{
System.out.println("Enter your character's strength.");
int st = input.nextInt();
return st;
}
public static int getAgi()
{
System.out.println("Enter your character's agility.");
int a = input.nextInt();
return a;
}
public static int getInt()
{
System.out.println("Enter your character's intellect.");
int i = input.nextInt();
return i;
}
public static void flee()
{

}

}

以下是示例输出,其中示例名称作为名字输入:

*~~~~~~~~~~~~~~*
{ ~ Pinnacle ~ }
*~~~~~~~~~~~~~~*
Enter your character's name.
Sample Name
Enter your character's stamina.
5
Enter your character's defense.
5
Enter your character's strength.
8
Enter your character's agility.
3
Enter your character's intellect.
2
*~~~~~~~~~~~~~~*
Sample Name
Stamina: 5
Defense: 5
Strength: 8
Agility: 3
Intellect: 2
*~~~~~~~~~~~~~~*
Confirm Character (Y/N)
n
Enter your character's name.
Enter your character's stamina.
2
Enter your character's defense.
3
Enter your character's strength.
6
Enter your character's agility.
4
Enter your character's intellect.
2
*~~~~~~~~~~~~~~*

Stamina: 2
Defense: 3
Strength: 6
Agility: 4
Intellect: 2
*~~~~~~~~~~~~~~*
Confirm Character (Y/N)
y
Character Created!

如您所见,在确认字符(Y/N)后出现问题。

最佳答案

嗯,我找到了解决您问题的方法,但我不确定为什么会出现您的问题。而不是使用 static Scanner input = new Scanner(System.in);在我们的 Player 类中,在您的每次获取中创建一个新的 Scanner*。

这里有一个建议:

public static Player createPlayerFromConsole() {
Scanner input = new Scanner(System.in);

System.out.println("Enter your character's name.");
String name = input.nextLine();

System.out.println("Enter your character's stamina.");
int stamina = input.nextInt();

System.out.println("Enter your character's defense.");
int defense = input.nextInt();

System.out.println("Enter your character's strength.");
int strength = input.nextInt();

System.out.println("Enter your character's agility.");
int agility = input.nextInt();

System.out.println("Enter your character's intellect.");
int intellect = input.nextInt();

return new Player(name, stamina, defense, strength, agility, intellect);
}

关于java - Scanner.nextLine();由于循环而在需要之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34938844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com