String 在while循环中使用hasNextInput()时,它会为给定的每个字符串提供循环,但我希望它提示一次 public void choice(){ 这个.damageCalculator(); story.typewriterefect(“给出您的攻击选择:\n”); 而(!input.hasNextInt()){ story.TypeWriteEffect(“请输入正确的数字(1、2或3)。(错误号INT)\n”); story.typewriterefect(“给出您的攻击选择:\n”); input.reset(); input.next(); } setUserChoiceOuttack(input.nextInt()); 如果(getUserChoiceOfAttack()>0&&getUserChoiceOfAttack()0&&player.getPlayerChoiceOfAttack()0&&getUserChoiceOfAttack()

String 在while循环中使用hasNextInput()时,它会为给定的每个字符串提供循环,但我希望它提示一次 public void choice(){ 这个.damageCalculator(); story.typewriterefect(“给出您的攻击选择:\n”); 而(!input.hasNextInt()){ story.TypeWriteEffect(“请输入正确的数字(1、2或3)。(错误号INT)\n”); story.typewriterefect(“给出您的攻击选择:\n”); input.reset(); input.next(); } setUserChoiceOuttack(input.nextInt()); 如果(getUserChoiceOfAttack()>0&&getUserChoiceOfAttack()0&&player.getPlayerChoiceOfAttack()0&&getUserChoiceOfAttack(),string,input,while-loop,java.util.scanner,String,Input,While Loop,Java.util.scanner,问题: 如果您在输入处输入“Lol nope”,则它将输出“请输入…”和“给出您的选择…”两次(对于键入的每个单词,用空格分隔)。 它运行得很好,但有一件事让我感到不安,那就是如果用户输入了他不应该输入的内容,我只希望它重复一次(即使是由空格分隔的多个字符串) 在这里开始编程,任何关于我的代码编写的反馈都非常感谢 编辑: pivu0的评论成功了! 这是我的固定代码,工作完美 public void choice(){ this.damageCalculator(); s

问题: 如果您在输入处输入“Lol nope”,则它将输出“请输入…”和“给出您的选择…”两次(对于键入的每个单词,用空格分隔)。 它运行得很好,但有一件事让我感到不安,那就是如果用户输入了他不应该输入的内容,我只希望它重复一次(即使是由空格分隔的多个字符串)

在这里开始编程,任何关于我的代码编写的反馈都非常感谢

编辑:

pivu0的评论成功了! 这是我的固定代码,工作完美

    public void choice(){

    this.damageCalculator();
    story.typeWriterEffect("Give your choice of attack: \n");

            while (!input.hasNextInt()){
                story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
                story.typeWriterEffect("Give your choice of attack: \n");
                input.reset();
                input.next();
            }
            setUserChoiceOfAttack(input.nextInt());

        if(getUserChoiceOfAttack() > 0 && getUserChoiceOfAttack() < 4){
            this.userAttackMoment();
        } else {
            story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n");
            this.choice();
            }
}
public void choice(){
这个.damageCalculator();
story.typewriterefect(“给出您的攻击选择:\n”);
布尔循环条件=真;
while(loopCondition==true){
试试{
player.setplayerchoiceoftack(Integer.parseInt(input.nextLine());
if(player.getPlayerChoiceOfAttack()>0&&player.getPlayerChoiceOfAttack()<4){
loopCondition=false;
}否则{
story.TypeWriteEffect(“请输入正确的数字(1,2/3)。(错误输入无效)\n”);
}
}
捕获(数字格式){
loopCondition=true;
story.TypeWriteEffect(“请输入正确的数字(1、2或3)。(错误号INT)\n”);
story.typewriterefect(“给出您的攻击选择:\n”);
}
}
这个.userAttackMoment();
}

使用布尔变量检查尝试

public void choice(){

    this.damageCalculator();

        story.typeWriterEffect("Give your choice of attack: \n");

        boolean loopCondition = true;
        while(loopCondition == true){

            try{                    
                player.setPlayerChoiceOfAttack(Integer.parseInt(input.nextLine()));

                if(player.getPlayerChoiceOfAttack() > 0 && player.getPlayerChoiceOfAttack() < 4){
                    loopCondition = false;
                } else {
                    story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n");
                    }
            }
            catch (NumberFormatException e){
                loopCondition = true;
                story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
                story.typeWriterEffect("Give your choice of attack: \n");
            }
        }
        this.userAttackMoment();
}
//尝试的初始值为false
公共无效选择(布尔尝试){
这个.damageCalculator();
story.typewriterefect(“给出您的攻击选择:\n”);
而(!input.hasNextInt()){
story.TypeWriteEffect(“请输入正确的数字(1、2或3)。(错误号INT)\n”);
story.typewriterefect(“给出您的攻击选择:\n”);
input.reset();
input.next();
}
setUserChoiceOuttack(input.nextInt());
如果(getUserChoiceOfAttack()>0&&getUserChoiceOfAttack()<4){
这个.userAttackMoment();
}否则,如果(!尝试){
story.TypeWriteEffect(“请输入正确的数字(1,2/3)。(错误输入无效)\n”);
这个选择(正确);
}
}

根据您使用的方法,我假设输入是
扫描仪的一个实例

如果您阅读了hasnetint的文档

您可以看到它检查下一个标记是否为int。对于scanner类,标记用空格分隔。因此它将检查您的第一个单词,得出“lol”不是int的结论,遍历循环,得出“nop”不是int的结论,再次遍历循环

尝试将整个扫描仪输入作为字符串
input.nextLine()
获取,并尝试使用
Integer.parseInt检查此字符串是否为整数

如果input.nextLine()是一个整数,它将退出循环。如果不是,则检查一个新输入:

//attempt is initally false
public void choice(boolean attempt){

    this.damageCalculator();

        story.typeWriterEffect("Give your choice of attack: \n");

            while (!input.hasNextInt()){
                story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
                story.typeWriterEffect("Give your choice of attack: \n");
                input.reset();
                input.next();
            }
                setUserChoiceOfAttack(input.nextInt());

        if(getUserChoiceOfAttack() > 0 && getUserChoiceOfAttack() < 4){
            this.userAttackMoment();
        } else if(!attempt){
            story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n");
            this.choice(true);
            }
}
int number=0;
boolean loopCondition = false;
while(!loopCondition){
 try {
    number = Integer.parseInt(input.nextLine())
    loopCondition = true;
    } 
 catch (NumberFormatException e){
    story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n");
            story.typeWriterEffect("Give your choice of attack: \n");
    loopCondition = false;
 }
}